No, it's not a feature of PyQt. Look at my previous post, I just edited it.
No, it's not a feature of PyQt. Look at my previous post, I just edited it.
That doesn't work for me. I only receive the MainThread print statement in my terminal.
edit: wysota, here is some modified code and the output. Even after the Object class is moved to the Thread class I still get the main thread's ID ... but for me the signal is not being caught by the Object's reportThread method.
Qt Code:
from PyQt4 import QtCore import sys, time def __init__(self): def reportThread(self): def __init__(self): def reportThread(self): thr = Thread() obj = Object() obj.moveToThread(thr) print obj, obj.thread(), obj.thread().currentThreadId() thr.start() timer.timeout.connect(obj.reportThread) timer.timeout.connect(thr.reportThread) timer.start(2000) t2.timeout.connect(app.quit) t2.start(10000) app.exec_()To copy to clipboard, switch view to plain text mode
output:
Qt Code:
<__main__.Object object at 0xabd2f8> <__main__.Thread object at 0xabd270> 140273089054464 MainThread 140273089054464 <__main__.Thread object at 0xabd270> 140273089054464 <__main__.Thread object at 0xabd270> 140273089054464 <__main__.Thread object at 0xabd270> 140273089054464 <__main__.Thread object at 0xabd270> 140273089054464 <__main__.Thread object at 0xabd270> 140273089054464To copy to clipboard, switch view to plain text mode
Last edited by prof.ebral; 11th December 2012 at 22:54.
That's because the Thread object lives in the main thread. You only moved the Object instance to the thread, not the Thread instance. There is a distinction between a thread (as in a separate processing flow) and QThread instance.
And how is that related to anything we've been discussing about? I don't care where the application runs. I care that a single thread doesn't have any problems in reading data from multiple sockets/ports.
What is poor design and how does having another thread prevent loss of some data that would otherwise magically be lost? Data is not lost if you read it 10ms later or 20ms later or even 2 seconds later. It's still there. What matters is that you read data fast enough to prevent cluttering the input buffer. And even that is nothing wrong if you are using TCP as the protocol itself is designed to prevent any data loss. Your serial port doesn't have that safety mechanism and thus you need to pay attention to read the data fast enough to not overrun the input buffer for the port. But it totally doesn't matter which thread performs the read.That is very poor design and not allowed in a scientific environment were loss of a single data point scraps the entire test run.
Strange. Somehow your flaws are "by design" (yeah yeah, it's a feature, not a bug) and something you consider a "flaw" in others' thinking is "unserious school project". Be serious. Any unresponsive system is badly designed. Period.It is a design requirement. Do not confuse this with some application on a machine which has any other purpose or ability in life.
Yes, you are trolling. Just read your posts.I'm not trolling or claiming to have all of the answers. I came in asking if there was unit test code proving this works. I got a bunch of responses which sounded like a Bing commercial.
So... QPushButton is also badly designed because it also has a disabled copy constructor? What about QSignalSpy itself? It also has a disabled copy constructor. So does every QObject-derived class. And so does QtSerialPort which is a sign of good design on behalf of its author.a point you have repeatedly failed to recognize.
I'm sorry but as a user of any user application I would not like to have any of my data lost too. And as a programmer of such applications I wouldn't want to have any data lost. Your system is by no means special.Please don't view this as a user application. This is a single purpose system running in an environment were no data point can be lost.
If you test with the signal tool, the signal will be emitted because the connection from the tool will make its emitter emit it. I think you fail to understand what I meant. Read the docs for QObject::receivers() to see what I meant and how does QSignalSpy influence it.Hence the wish to test with the signal tool.
No. My projects never loose any signals, they don't loose any "data points" nor anything like that. They often use multithreading and classes from them are often thread-safe. So please talk about your projects, not about others'.Projects which get released for use with Qt.
Thank you, your Grace, for giving us your permissionThis message thread can now end.
You subclassed QThread. I didn't.
Look, the essential difference between code from posts #25 (mine) and #28 (yours) is that the code in post #28 subclasses QThread and connects to a slot from an instance of this class (lines 11-16 and 28). This is why you get the id of the main thread --- because QThread instance lives in the main thread. If you always do that in your code (meaning you connect a signal to a slot in the QThread subclass) then indeed all slots are executed in the context of the main thread, because that's where the QThread subclass instance lives. The modification you did in post #28 to my code from post #25 is what makes the difference between your output and mine.
Wysota ... the Object doesn't even execute the reportThread method in your code, nor in mine. I'm trying to point that out to you. Like I said, you have a hard time reading.
Here ... I will quote my post:
"That doesn't work for me. I only receive the MainThread print statement in my terminal.
edit: wysota, here is some modified code and the output. Even after the Object class is moved to the Thread class I still get the main thread's ID ... but for me the signal is not being caught by the Object's reportThread method."
I understand that it doesn't work for you, I don't know why. It works for me just fine and I'm sure it works for many other people because it is intended to work that way.
You get the main thread ID's because you connect the timer's signal to the slot of an object that lives in main thread.Even after the Object class is moved to the Thread class I still get the main thread's ID ... but for me the signal is not being caught by the Object's reportThread method."
I just tried running the code you posted but it bails out with a syntax error (I'm using Python 3):
text Code:
File "./code.py", line 11 print self, (QtCore.QThread.currentThreadId()) ^To copy to clipboard, switch view to plain text mode
After fixing those syntax errors (by putting print arguments in parenthesis) and running the code, I get this:
<__main__.Object object at 0x7fa7c3b50b00> <__main__.Thread object at 0x7fa7c3b50a70> 140358524008192
MainThread 140358524008192
<__main__.Thread object at 0x7fa7c3b50a70> 140358524008192
<__main__.Object object at 0x7fa7c3b50b00> 140358466680576
<__main__.Thread object at 0x7fa7c3b50a70> 140358524008192
<__main__.Object object at 0x7fa7c3b50b00> 140358466680576
You can see two slots are called -- one in Object (with thread id 576) and the other one in Thread (with thread id 192). That's the correct behaviour as described in Qt docs (PyQt can't possibly work differently because it's just a wrapper over Qt C++ classes). I don't know why you're getting no output for Object, the most obvious reason is that the worker thread is not started (or its event loop is not running).
A side note: I checked with Python2.7 and it works the same way as with Python3:
(<__main__.Object object at 0x7fa08aaf7d40>, <__main__.Thread object at 0x7fa08aaf7cb0>, 140327479097088L)
MainThread 140327479097088
(<__main__.Thread object at 0x7fa08aaf7cb0>, 140327479097088L(<__main__.Object object at 0x7fa08aaf7d40>, 140327422002944L)
)
(<__main__.Thread object at 0x7fa08aaf7cb0>, 140327479097088L)
(<__main__.Object object at 0x7fa08aaf7d40>, 140327422002944L)
(<__main__.Thread object at 0x7fa08aaf7cb0>, 140327479097088L)
(<__main__.Object object at 0x7fa08aaf7d40>, 140327422002944L)
Bookmarks