multitask

multitask allows Python programs to use generators (aka coroutines) to perform cooperative multitasking and asynchronous I/O.
Download

multitask Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Chris Stawarz
  • Publisher web site:
  • http://o2s.csail.mit.edu/o2s-wiki/multitask

multitask Tags


multitask Description

multitask allows Python programs to use generators (aka coroutines) to perform cooperative multitasking and asynchronous I/O. multitask allows Python programs to use generators (aka 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.This project is free software, distributed under the MIT license.Examples:As a very simple example, here's how one could use multitask to allow two unrelated tasks to run concurrently:>>> def printer(message):... while True:... print message... yield... >>> multitask.add(printer('hello'))>>> multitask.add(printer('goodbye'))>>> multitask.run()hellogoodbyehellogoodbyehellogoodbyeFor a more useful example, here's how one could implement a multitasking server that can handle multiple concurrent client connections:def listener(sock): while True: conn, address = (yield multitask.accept(sock)) multitask.add(client_handler(conn))def client_handler(sock): while True: request = (yield multitask.recv(sock, 1024)) if not request: break response = handle_request(request) yield multitask.send(sock, response)multitask.add(listener(sock))multitask.run()The functions and classes in the multitask module allow tasks to yield for I/O operations on sockets and file descriptors, adding/removing data to/from queues, or sleeping for a specified interval. When yielding, a task can also specify a timeout. If the operation for which the task yielded has not completed after the given number of seconds, the task is restarted, and a Timeout exception is raised at the point of yielding.Tasks can also yield other tasks, which allows for composition of tasks and reuse of existing multitasking code. A child task runs until it either completes or raises an exception, and its output or exception is propagated to its parent. For example:>>> def parent():... try:... print 'good child says: %s' % (yield child())... print 'bad child says: %s' % (yield child(bad=True))... except Exception, e:... print 'caught exception: %s' % e... >>> def child(bad=False):... if bad:... raise RuntimeError('oops!')... yield 'Hi, Mom!'... >>> multitask.add(parent())>>> multitask.run()good child says: Hi, Mom!caught exception: oops!Requirements:· Python 2.5 or laterWhat's New in This Release:· Child tasks now return values to their parent by raising StopIteration with the return values as arguments.· get_default_task_manager() has been added to provide access to the default TaskManager instance used by add() and run().· readline() has been added, which (on Unix-like systems) is useful for doing non-blocking reads from the stdout of a child process.


multitask Related Software