[Twisted-Python] spawning tasks after reactor?

Darren Govoni darren at ontrenet.com
Wed Dec 31 15:08:25 EST 2008


Hi,
  Thanks for that suggestion. It looks very close to what I need. I
tried doing it _after_ the reactor is run and it did not execute. Here
is a code snippet.

def convert_to_pdf(arg):
   print "converting..."

def notify_pdf:
   print "Yay! I'm done"

...
		d = threads.deferToThread(convert_to_pdf,offset)
		d.addCallback(notify_pdf)

All the examples for threads.deferToThread call reactor.run() after
configuring all the threads, but this isn't useful in my app.

If I call reactor.run() and _then_ try to deferToThread, it doesn't
work. I'm probably missing an argument or something.

Thanks for any tips. Much appreciated.

Darren

On Wed, 2008-12-31 at 11:16 -0500, Jean-Paul Calderone wrote:
> On Wed, 31 Dec 2008 10:51:38 -0500, Darren Govoni <darren at ontrenet.com> wrote:
> >Hi Jean-Paul,
> >  Thank you for those suggestions. I can post some simple code after I
> >get further. The psuedo code of what I want to do is like this:
> >every few seconds, check a message queue (I see how this is done):
> >   if there is a message, spawn a deferred so I don't block:
> >	# The deferred should immediately call a method in a separate
> >	# thread (wisted kind) to process the message
> >	# Allow for 10 or so deferreds to be processing messages
> >	# in parallel while the reactor continues to receive them
> >	# but block scheduling the deferred if the pool is maxed
> >	# out
> 
> A Deferred is just a convenient way to keep track of callbacks.  It's
> not an active thing, so to "spawn" one doesn't make much sense.  Instead,
> you spawn something else and use a Deferred to keep track of its result.
> 
> >
> >Sorry if this is lacking in detail, i'm trying to test the idea in a
> >simple way with twisted first. If I set up a deferred before calling
> >reactor.run(), then of course, it runs, but after reactor.run() I have
> >to invoke reactor.callLater(...) so its a bit different, but what I'd
> >like is something like reactor.callNow(...) that doesn't block and
> >invokes the generator in a thread pool.
> 
> You probably want twisted.internet.threads.deferToThreadPool (or the older
> deferToThread).  From its docstring:
> 
>   Call the function C{f} using a thread from the given threadpool and return
>   the result as a Deferred.
> 
> Jean-Paul
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list