multitask 0.2.0: Cooperative multitasking and asynchronous I/O using generators

Christopher Stawarz cstawarz at csail.mit.edu
Mon Jun 11 16:47:41 CEST 2007


multitask allows Python programs to use generators (a.k.a. coroutines)
to perform cooperative multitasking and asynchronous I/O.
Applications written using multitask consist of a set of cooperating
tasks that yield to a shared task manager whenever they perform a
(potentially) blocking operation, such as I/O on a socket or getting
data from a queue.  The task manager temporarily suspends the task
(allowing other tasks to run in the meantime) and then restarts it
when the blocking operation is complete.  Such an approach is suitable
for applications that would otherwise have to use select() and/or
multiple threads to achieve concurrency.

multitask 0.2.0 includes the following changes:

* Child tasks now return values to their parent by raising
   StopIteration with the return values as arguments:

     >>> def parent():
     ...     print (yield return_none())
     ...     print (yield return_one())
     ...     print (yield return_many())
     ...
     >>> def return_none():
     ...     yield
     ...     # implicit StopIteration here
     ...
     >>> def return_one():
     ...     yield
     ...     raise StopIteration(1)
     ...
     >>> def return_many():
     ...     yield
     ...     raise StopIteration(2, 3)
     ...
     >>> multitask.add(parent())
     >>> multitask.run()
     None
     1
     (2, 3)

   This mechanism was suggested by Dustin Mitchell [1] and provides a
   cleaner, more explicit way of returning values than the mechanism
   used in multitask 0.1.0 (which was to assume that the last value
   yielded by a child was its return value).

   [1] http://mail.python.org/pipermail/python-list/2007-May/438370.html

* Added get_default_task_manager() to provide access to the default
   TaskManager instance used by add() and run()

* Added readline(), which (on Unix-like systems) is useful for doing
   non-blocking reads from the stdout of a child process


For more information:
   http://o2s.csail.mit.edu/o2s-wiki/multitask

To download:
   http://o2s.csail.mit.edu/download/multitask/


Cheers,
Chris Stawarz


More information about the Python-announce-list mailing list