diff options
| author | Denis Bilenko <denis@ag-projects.com> | 2008-10-08 19:20:42 +0700 |
|---|---|---|
| committer | Denis Bilenko <denis@ag-projects.com> | 2008-10-08 19:20:42 +0700 |
| commit | bc64606a6c38153673763904b13ef3f3341ef355 (patch) | |
| tree | 0af47fb5b5c8dd36b2e34892720f0fa70fc13785 /examples | |
| parent | b28fb130c80eda36a455ad341d1575c33d48ce6d (diff) | |
| download | eventlet-bc64606a6c38153673763904b13ef3f3341ef355.tar.gz | |
example on how to use twisted server + green socket client in the same process
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/twisted_ex1.py | 38 |
1 files changed, 38 insertions, 0 deletions
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() |
