Skip to main content

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

  • epoll, kqueue, select, i/o completion ports behind the scene

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

  • 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

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

@coroutine
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")
            sock.close()
            return
        yield sock.write(line)

m = schedulers.Scheduler()
m.add(server)
m.run()

Project details


Download files

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

Source Distribution

cogen-0.1.7.zip (246.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cogen-0.1.7-py2.5.egg (109.3 kB view details)

Uploaded Egg

File details

Details for the file cogen-0.1.7.zip.

File metadata

  • Download URL: cogen-0.1.7.zip
  • Upload date:
  • Size: 246.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cogen-0.1.7.zip
Algorithm Hash digest
SHA256 64137e8fb6612b8fde89b757805a922d8a20ce709e032d64d275559a61bc7a64
MD5 e0fa7e06fef99f12b829c2a8855192e6
BLAKE2b-256 720c04fc087f595b3f1d266b19988ed2c411b8c86bb8cae7ea31a85002d06a14

See more details on using hashes here.

File details

Details for the file cogen-0.1.7-py2.5.egg.

File metadata

  • Download URL: cogen-0.1.7-py2.5.egg
  • Upload date:
  • Size: 109.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cogen-0.1.7-py2.5.egg
Algorithm Hash digest
SHA256 81c0767c65213d69a655da9e1dceae27084f1ed55ae3e5b0815974a76dc62fee
MD5 fb5f3b1d6d027242f326b4e230407d30
BLAKE2b-256 e8fbe34ee6456f0285804e522017c910910b3513932386d5acbfe1da99391150

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page