Reply
Thread Tools
Posts: 3,428 | Thanked: 2,856 times | Joined on Jul 2008
#21
Originally Posted by attila77 View Post
Hang on... these are two things here. Events/signals/slots across multiple threads very much exist in Qt (you do need to know what you're doing, though, and whether you're using a module/class/function that (probably due to performance reasons) requires additional synchronisation - this is very visibly displayed in the Qt docs of each class). GIL issues and blocking Python calls are a completely separate issue - threads are a special story under Python even without Qt.
was that commenting to me?

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!
 
Posts: 13 | Thanked: 20 times | Joined on Jun 2010
#22
I added some stuff to the wiki. I added some notes to the list of apps so if someone's looking for a specific technique they can quickly see how someone else implemented it. I'm just quickly scanning the code so I'm easily missing stuff.

I'm wondering if we could get some simple, yet working examples of threading. I suppose there's a few types of threading - a thread that goes off and does it's own thing, and only reports back when it's done and a thread that goes off and does it's own thing and then sends data of varying types back to the main thread. I have some code that I wrote for a project that goes and does an a web-based API query and downloads an image set and then notifies the parent of completion and the list of images, but I'm not sure the code is 'safe' or the greatest.

I've also made a custom widget and wondering if people'd be interested in some quick documentation on that. I found 3 or 4 articles on making widgets in pyqt, but none of them seemed to be individually accurate, it was merging various parts and a lot of trial and error to get things working properly.
 
Posts: 3,428 | Thanked: 2,856 times | Joined on Jul 2008
#23
Here's something:

Code:
#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_())
output:
Code:
~/test $ python thread.py
thread1 completed.
thread3 completed.
thread2 completed.
thread4 completed.
~/test $
__________________
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!

Last edited by fatalsaint; 2010-06-17 at 17:39.
 

The Following User Says Thank You to fatalsaint For This Useful Post:
Khertan's Avatar
Posts: 1,012 | Thanked: 817 times | Joined on Jul 2007 @ France
#24
I use qthread too on Khweeteur for retrieving status from twitter (http://gitorious.org/khweeteur) (http://khertan.net/khweeteur)

But just an advice, do not start to many thread at the same time on your device ... it slow down more the whole process than reducing time. The main loop and one thread to not block the ui is enough
 
Reply


 
Forum Jump


All times are GMT. The time now is 10:08.