Re: [Twisted-Python] Threading examples...
Thanks, So, now my example looks like this: import time from twisted.internet import reactor def aSillyBlockingMethod(x): time.sleep(2) print x reactor.callFromThread(reactor.stop) # run method in thread reactor.callInThread(aSillyBlockingMethod, "2 seconds have passed") It still just hangs... I have to go kill it from another window after getting its pid from ps. Am I doing the reactor.stop thing correctly? Basically, I have an application that talks via a http interface to a user and I need to spawn off and do stuff from time to time. I'm still learning how all the reactor eventloops and whatnot work. I ran into a problem where I'm trying to run something via twisted.internet.protocol.ProcessProtocol and serve its results up from within a reactor.listentTCP() page handler ... but it blew up. So, I'm thinking that I could maybe just fork it off and run that way. I've been trying to figure out how the reactors() work and the threading and whatnot the last few days but am kind of finding little help in the docs and google that is at my level of understanding... seems most of the stuff is snippets and pieces that assume you already see the big picture :-( Any help here is greatly appreciated ;-) Thanks, -William
From: Andrew Bennetts <andrew-twisted@puzzling.org> Reply-To: twisted-python@twistedmatrix.com To: twisted-python@twistedmatrix.com Subject: Re: [Twisted-Python] Threading examples... Date: Wed, 24 Mar 2004 09:30:13 +1100
On Tue, Mar 23, 2004 at 12:19:01PM -0700, William McLendon wrote:
Hi,
I'm trying out some of the threading examples from the web and am getting some a lockup at the end.
Here's the code I'm playing with, I pulled it straight from the examples:
(source: http://twisted.sourceforge.net/TwistedDocs-1.2.0/howto/threading.html)
#!/usr/bin/env python import time from twisted.internet import reactor
def aSillyBlockingMethod(x): import time time.sleep(2) print x
# run method in thread reactor.callInThread(aSillyBlockingMethod, "2 seconds have passed")
This example doesn't call to reactor.stop(), so it never stops. Put a call to "reactor.callFromThread(reactor.stop)" after the print statement, for example, and it should shutdown cleanly.
Is there something I need to do that will make the thread go away? Are there some more examples of threading in Twisted-Python available?
The only example I can think of is the implementation of twisted.enterprise.adbapi, which presents an asynchronous interface to blocking DB-API modules by running it in threads.
-Andrew.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_________________________________________________________________ Is your PC infected? Get a FREE online computer virus scan from McAfee® Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
On Tue, Mar 23, 2004 at 03:58:20PM -0700, William McLendon wrote:
Thanks,
So, now my example looks like this:
import time from twisted.internet import reactor
def aSillyBlockingMethod(x): time.sleep(2) print x reactor.callFromThread(reactor.stop)
# run method in thread reactor.callInThread(aSillyBlockingMethod, "2 seconds have passed")
It still just hangs... I have to go kill it from another window after getting its pid from ps. Am I doing the reactor.stop thing correctly?
Heh. I just noticed the real bug: that example doesn't call reactor.run(), either. Complete, working example: ---- import time from twisted.internet import reactor def aSillyBlockingMethod(x): time.sleep(2) print x reactor.callFromThread(reactor.stop) # run method in thread reactor.callInThread(aSillyBlockingMethod, "2 seconds have passed") reactor.run() ---- That reactor.callInThread starts the thread when the reactor isn't running yet is probably a bug in Twisted.
Basically, I have an application that talks via a http interface to a user and I need to spawn off and do stuff from time to time. I'm still learning how all the reactor eventloops and whatnot work. I ran into a problem where I'm trying to run something via twisted.internet.protocol.ProcessProtocol and serve its results up from within a reactor.listentTCP() page handler ... but it blew up. So, I'm
How did it blow up (and how are you spawning the process)? Twisted is quite capable of coping with this -- it's how the CGI support in twisted.web works.
thinking that I could maybe just fork it off and run that way. I've been trying to figure out how the reactors() work and the threading and whatnot the last few days but am kind of finding little help in the docs and google that is at my level of understanding... seems most of the stuff is snippets and pieces that assume you already see the big picture :-( Any help here is greatly appreciated ;-)
I strongly advise avoiding threads unless you really need them. From your description of what you are trying to do, you don't need them. -Andrew.
participants (2)
-
Andrew Bennetts
-
William McLendon