|
|
2010-06-17
, 14:46
|
|
Posts: 13 |
Thanked: 20 times |
Joined on Jun 2010
|
#22
|
|
|
2010-06-17
, 17:36
|
|
Posts: 3,428 |
Thanked: 2,856 times |
Joined on Jul 2008
|
#23
|
#imports, yay!
import sys, time
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
#create thread class
class thread_signal( QThread ):
#define signals
__pyqtSignals__ = ( "thread_sig", "close" )
def __init__ ( self, ktime, name ):
#ktime is how long to sleep for
#name is a name for the thread.
self.ktime = ktime
self.name = name
QThread.__init__ ( self )
def run( self ):
#night night
time.sleep(self.ktime )
#good morning!
self.emit( SIGNAL("thread_sig"), str(self.name))
if self.name == "thread4":
#without calling app.exit, your python app will hang forever and ever. So we do this on the last thread.
self.emit ( SIGNAL("close") )
#output is always good..
def output( name=None ):
print name + " completed."
if __name__ == "__main__":
app=QCoreApplication(sys.argv)
#create threads
thread1=thread_signal(1, "thread1")
thread2=thread_signal(3, "thread2")
thread3=thread_signal(2, "thread3")
thread4=thread_signal(4, "thread4")
#connect signals
QObject.connect(thread1, SIGNAL("thread_sig"), output)
QObject.connect(thread2, SIGNAL("thread_sig"), output)
QObject.connect(thread3, SIGNAL("thread_sig"), output)
QObject.connect(thread4, SIGNAL("thread_sig"), output)
QObject.connect(thread4, SIGNAL("close"), app.exit)
#start threads
thread1.start()
thread2.start()
thread3.start()
thread4.start()
#start application, waiting for threads to complete.
sys.exit(app.exec_())
~/test $ python thread.py thread1 completed. thread3 completed. thread2 completed. thread4 completed. ~/test $
| The Following User Says Thank You to fatalsaint For This Useful Post: | ||
|
|
2010-08-21
, 06:11
|
|
|
Posts: 1,012 |
Thanked: 817 times |
Joined on Jul 2007
@ France
|
#24
|
I create a separate class of QThread type and run it. I then put my blocking calls (urllib, xmlrpc, etc) inside that thread and when they are finished, they throw up and emit signals that my main UI thread is connected to.
Like I said.. I have no problems with Threads, cross-thread signaling, or anything of that nature in my apps.
If I've helped you or you use any of my packages feel free to help me out.
-----------------------------------------------------------------------------------
Maintaining:
pyRadio - Pandora Radio on your N900, N810 or N800!