My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Overview
--------
This is a library for network oriented, coroutine based programming.

*cogen*'s goal is to enable writing code in a seemingly 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, sendfile behind the scenes
* a couple of useful classes for putting the coroutine to sleep, wait for
signals, queues, timeouts etc.


Quick introduction
==================
A coroutine is just a generator wrapped in a helper class:

::

from cogen.core.coroutines import coroutine

@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.coroutines 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()


Documentation
=============

http://cogen.googlecode.com/svn/trunk/docs/build/index.html

Development
============

Takes place at: http://code.google.com/p/cogen/

Grab the latest and greatest from `trunk <http://cogen.googlecode.com/svn/trunk/#egg=cogen-dev>`_ with::

easy_install cogen==dev
Show details Hide details

Change log

r552 by ionel.mc on Dec 30, 2008   Diff
updated the project metadata a bit
Go to: 
Project members, sign in to write a code review

Older revisions

r547 by ionel.mc on Dec 26, 2008   Diff
fixed a rst markup error
r528 by ionel.mc on Dec 19, 2008   Diff
updated news and readme
r389 by ionel.mc on May 02, 2008   Diff
updated README.txt
All revisions of this file

File info

Size: 2526 bytes, 90 lines
Hosted by Google Code