Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • BackTrack 5 R2
  • Wine 1.4 / 1.5.5
  • Mozilla Firefox 12...
  • Ubuntu 11.04
  • Angry Birds 1.1.2.1
  • Ubuntu 10.04.4 LTS
  • Linux Kernel 3.4
  • Ubuntu Manual 10.10
  • Adobe Flash Player...
  • Pidgin 2.10.4
  • Home > Linux > System > Networking

    multitask 0.2.0

    Download button

    No screenshots available
    Downloads: 383  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Chris Stawarz | More programs
    MIT/X Consortium Lic... / FREE
    June 12th, 2007, 10:35 GMT
    ROOT / System / Networking

     Read user reviews (0)  Refer to a friend  Subscribe

    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()
    hello
    goodbye
    hello
    goodbye
    hello
    goodbye
    [and so on ...]

    For 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 later

    What'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.



    Product's homepage

      


    TAGS:

    cooperative multitasking | asynchronous I/O | Python development | multitask | cooperative | multitasking



    HTML code for linking to this page:


    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM