Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
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


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 receive 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 compliant, 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:

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.
  • 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

from cogen.core import sockets
from cogen.core import schedulers
from cogen.core.coroutine import coroutine

def server():
    srv = sockets.Socket()
    print type(srv)
    while 1:
        print "Listening..."
        conn, addr = yield srv.accept()
        print "Connection from %s:%s" % addr
        m.add(handler, args=(conn, addr))

def handler(sock, addr):
    yield sock.write("WELCOME TO ECHO SERVER !\r\n")

    while 1:
        line = yield sock.readline(8192)
        if line.strip() == 'exit':
            yield sock.write("GOOD BYE")
        yield sock.write(line)

m = schedulers.Scheduler()

Release History

History Node


History Node


History Node

History Node


History Node


History Node


This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

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

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(99.9 kB) Copy SHA256 Hash SHA256
2.5 Egg Feb 23, 2008
(243.6 kB) Copy SHA256 Hash SHA256
Source Feb 23, 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