Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Coroutines and asynchronous I/O using enhanced generators from python 2.5, including a enhanced WSGI server.

Project Description

Overview

This is a library for network oriented, coroutine based programming.

cogen’s goal is to enable writing code in a synchronous and easy manner in the form of generators that yield calls and recieve the result from that yield. These calls translate to asynchronous and fast os calls in cogen’s internals.

Notable features

  • a WSGI server, HTTP1.1 compilat, with asynchronous extensions
  • use epoll, kqueue where supported, select based otherwise
  • a Queue with the same interface as the standard library Queue, but for coroutines

Quick introduction

Programming with cogen library should be straightforward, similar with programming threads but without all the problems. A coroutine is just a generator wrapped in a operation handling class:

@coroutine
def mycoro(bla):
    result = yield <operation>
    result = yield <operation>
  • the operation instructs the scheduler what to do with the coroutine: suspend it till something happens, add another coro in the scheduler, raise a event and so on.
  • the operations are split up in 2 modules: events and sockets * the operations from sockets are related to network, like reading and writing, and these are done asynchronously but your code in the coroutine will see them as a regular synchronous or blocking call.
  • the operations from events are related to signals and coroutine/scheduler management.
  • if a operation has a result associated then the yield will return that result (eg. a string or a (connection, address) tuple) otherwise it will return the operation instance.

Echo server example

@coroutine
def server():
    srv = sockets.Socket()
    srv.setblocking(0)
    srv.bind(('localhost',777))
    srv.listen(10)
    while 1:
        print "Listening..."
        conn, addr = yield sockets.Accept(srv)
        print "Connection from %s:%s" % addr
        m.add(handler, conn, addr)

@coroutine
def handler(sock, addr):
    yield sockets.Write(sock, "WELCOME TO ECHO SERVER !\r\n")

    while 1:
        line = yield sockets.ReadLine(sock, 8192)
        if line.strip() == 'exit':
            yield sockets.Write(sock, "GOOD BYE")
            sock.close()
            return
        yield sockets.Write(sock, line)

m = Scheduler()
m.add(server)
m.run()
Release History

Release History

History Node

0.2.1

History Node

0.2.0

History Node

0.1.9.1

History Node

0.1.9

History Node

0.1.8

History Node

0.1.7

History Node

0.1.6

This version
History Node

0.1.5

History Node

0.1.4

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

History Node

0.0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
cogen-0.1.5-py2.5.egg (86.6 kB) Copy SHA256 Checksum SHA256 2.5 Egg Jan 21, 2008
cogen-0.1.5.zip (201.5 kB) Copy SHA256 Checksum SHA256 Source Jan 21, 2008

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting