From bc64606a6c38153673763904b13ef3f3341ef355 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Wed, 8 Oct 2008 19:20:42 +0700 Subject: [PATCH] example on how to use twisted server + green socket client in the same process --- examples/twisted_ex1.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/twisted_ex1.py diff --git a/examples/twisted_ex1.py b/examples/twisted_ex1.py new file mode 100644 index 0000000..1ff394a --- /dev/null +++ b/examples/twisted_ex1.py @@ -0,0 +1,38 @@ +from twisted.internet import pollreactor; pollreactor.install() +from twisted.internet.protocol import Protocol, Factory +from twisted.internet import reactor + +from eventlet.green import socket +from eventlet.api import spawn +from eventlet import coros + +class Echo(Protocol): + def dataReceived(self, data): + print 'server received: %r' % data + self.transport.write('you said %s' % data) + +factory = Factory() +factory.protocol = Echo +reactor.listenTCP(34567, factory) + +N=4 +count = coros.metaphore() +count.inc(N) + +def client(): + try: + c = socket.socket() + c.connect(('127.0.0.1', 34567)) + c.send('hello') + print 'client received: %s' % c.recv(1024) + c.send('bye') + print 'client received: %s' % c.recv(1024) + finally: + count.dec() + +for x in range(N): + # note that spawn doesn't switch to new greenlet immediately. + spawn(client) + +# the execution ends with the main greenlet's exit (by design), so we need to pause here +count.wait()