[Twisted-Python] Writing a wrapper from asynchronous to synchronous

Brett Viren bv at bnl.gov
Fri Feb 24 11:20:52 EST 2006


"Uwe C. Schroeder" <uwe at oss4u.com> writes:

> 1) module calls some method #1
> 2) #1 depends on certain initializations to finish
> 3) the call to #1 doesn't return unless step 2) is done

I've not yet learned PB, so maybe this isn't a good fit but what about
having modules run in threads and use Queues for them to send and
receive messages (method calls / return values) to/from the framework?

This way they won't block the framework but will appear to run
synchronously.  You could hide the Queue manipulation in a thin API
that shadows the API of the framework.

The timeline would be:

 Client calls wrapper function

 Function builds message, sends it to nonblocking framework queue and
 then listens on blocking reply queue.

 Framework checks input queue in nonblocking way.  When it recieves a
 message it calls the corresponding asynchronous method and attaches
 code to the callback that will send the reply down the reply queue.

 Wrapper function gets reply, unpacks return and returns it to
 synchronous caller.

I've used a similar pattern for doing the opposite of sending commands
from async framework to syncronous client code.


-Brett.





More information about the Twisted-Python mailing list