Corotwine 0.1
I've just released Corotwine, a set of Coroutine APIs for Twisted. You can download Corotwine-0.1.tar.gz. There is very thorough API documentation.
Here's a complete example chat server. Start it up and telnet to port 1025 a couple of times and you'll be able to chat.
from twisted.internet import reactor
from twisted.internet.error import ConnectionClosed
from corotwine.protocol import gListenTCP, LineBuffer
class Chat(object):
def __init__(self):
self.clients = []
def handleConnection(self, transport):
transport = LineBuffer(transport)
self.clients.append(transport)
try:
try:
for line in transport:
for client in self.clients:
if client is not transport:
client.writeLine(line)
finally:
self.clients.remove(transport)
except ConnectionClosed:
return
from twisted.python.log import startLogging
import sys
startLogging(sys.stdout)
gListenTCP(1025, Chat().handleConnection)
reactor.run()
There are more examples in the corotwine/examples.py file.
5 comments:
awesome :)
Cool. I've been thinking about this for a while. Good to see someone's releasing something.
I see you require Greenlet, and talk about Stackless as a future possibility. Why not just use standard python 2.5 coroutines?
Sorry Ryan, Python 2.5 doesn't have coroutines. What some may say (including in PEPs) is a lie, and a misuse of the term.
Christopher Armstrong: half-baked cookie is still a cookie right ?
The enhanced generators in 2.5 can be used to implement coroutines (with limitations, but regardless, still coroutines)
It's not just a matter of having a poor implementation (being "half-baked") - the thing that you can implement with Python 2.5 generators that you insist on calling "coroutine" has _very_ different semantics and use cases than real coroutines.
With real coroutines, you can cause arbitrary code to switch contexts. With generator-"coroutines", *every frame* needs to know about context switches. This is a huge difference in how they're used and what they can do.
Post a Comment