Now I'm getting some very interesting results.
First run output:
GUI CLASS INITIALIZED!!!
Done in 2.99009998083
QThread: Destroyed
while thread is still running
QThread: Destroyed
while thread is still running
QThread: Destroyed
while thread is still running
QThread: Destroyed
while thread is still running
QThread: Destroyed
while thread is still running
GUI CLASS INITIALIZED!!!
Done in 2.99009998083
QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
To copy to clipboard, switch view to plain text mode
Second run output:
GUI CLASS INITIALIZED!!!
UNUSEDIDS [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
UNUSEDIDS [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
QThread: Destroyed
while thread is still running
QThread: Destroyed
while thread is still running
UNUSEDIDS [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Done in 13.9999389648
GUI CLASS INITIALIZED!!!
UNUSEDIDS [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 0
UNUSEDIDS [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 1
QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
UNUSEDIDS [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 2
Done in 13.9999389648
To copy to clipboard, switch view to plain text mode
Third run output
GUI CLASS INITIALIZED!!!
UNUSEDIDS UNUSEDIDS[ UNUSEDIDS0[ , 0[1, 0, 1, 2, 1, 2, 3UNUSEDIDS, 2, 3, 4[, 3, 04, 5UNUSEDIDS, , 4, Done in 2.00009346008 15, 6
[, , 5, 026, 7, , , 6,
GUI CLASS INITIALIZED!!!
UNUSEDIDS UNUSEDIDS[ UNUSEDIDS0[ , 0[1, 0, 1, 2, 1, 2, 3UNUSEDIDS, 2, 3, 4[, 3, 04, 5UNUSEDIDS, , 4, Done in 2.00009346008 15, 6
[, , 5, 026, 7, , , 6,
To copy to clipboard, switch view to plain text mode
Fourth run output
GUI CLASS INITIALIZED!!!
UNUSEDIDS [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
UNUSEDIDS [1, 2, 3, 4, 5, 6, UNUSEDIDS7 , [Done in 3.9999485015981
, ,
QThread: Destroyed
while thread is still running
QThread: Destroyed
while thread is still running
92, , 10
GUI CLASS INITIALIZED!!!
UNUSEDIDS [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 0
UNUSEDIDS [1, 2, 3, 4, 5, 6, UNUSEDIDS7 , [Done in 3.9999485015981
, , QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
92, , 10
To copy to clipboard, switch view to plain text mode
Fifth run output the python.exe crashed (Windows displayed the "not responding" message).
What's obvious is that there are different results from every single run. This prompted me to believe that either there's a problem with GC, or that there's an actual bug in Qt.
It crossed my mind to do the following:
self.workers = [Worker2(),Worker2(),Worker2(),Worker2(),Worker2()]
for i in xrange(5):
self.workers[i].start()
time.sleep(0.01)
self.workers = [Worker2(),Worker2(),Worker2(),Worker2(),Worker2()]
for i in xrange(5):
self.workers[i].start()
time.sleep(0.01)
To copy to clipboard, switch view to plain text mode
In other words, sleep for 10 miliseconds after every thread start. Interestingly enough, this (sort-of) worked. Here's the result I got every time after 5 runs:
GUI CLASS INITIALIZED!!!
UNUSEDIDS [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
UNUSEDIDS [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
UNUSEDIDS [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
UNUSEDIDS [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
UNUSEDIDS [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Done in 49.9999523163
GUI CLASS INITIALIZED!!!
UNUSEDIDS [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 0
UNUSEDIDS [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 1
UNUSEDIDS [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 2
UNUSEDIDS [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 3
UNUSEDIDS [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Hello world from QThread 4
Done in 49.9999523163
To copy to clipboard, switch view to plain text mode
While not quite as efficient as the QRunnable equivalent (which gets done in 10 ms but this is because the QRunnable's for loop does not have the time.sleep at the end), it's better than nothing. Since my application will be limited to 20 (30) threads at most, the performance impact should be negligible.
I've also noticed that if I reduce the sleep() period to 1 milisecond, the code becomes much more unstable; while it worked fine the first 3 runs, it crashed the interpreter on the 4th run. I am assuming that the higher the value of the sleep is, the more stable the code will be.
Bookmarks