From 2948ef52aedcd98edd344c6d4b2bf9375ccb9f6d Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Mon, 12 Jan 2009 16:27:37 +0600 Subject: [PATCH] brought examples up-to-date --- examples/connect.py | 24 ++++++++++++++---------- examples/twisted_http_proxy.py | 1 + examples/twisted_portforward.py | 9 ++++----- examples/twisted_server.py | 1 + examples/twisted_srvconnector.py | 7 +------ 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/examples/connect.py b/examples/connect.py index 921d5c7..d7ff1b0 100644 --- a/examples/connect.py +++ b/examples/connect.py @@ -1,27 +1,31 @@ -"""Spawn multiple greenlet-workers and collect their results. +"""Spawn multiple workers and collect their results. -Demonstrates how to use eventlet.green package and coros.Job. +Demonstrates how to use eventlet.green package and proc module. """ +from eventlet import proc from eventlet.green import socket -from eventlet.coros import Job # this example works with both standard eventlet hubs and with twisted-based hub -# comment out the following line to use standard eventlet hub -from twisted.internet import reactor +# uncomment the following line to use twisted hub +#from twisted.internet import reactor def geturl(url): c = socket.socket() ip = socket.gethostbyname(url) c.connect((ip, 80)) + print '%s connected' % url c.send('GET /\r\n\r\n') return c.recv(1024) urls = ['www.google.com', 'www.yandex.ru', 'www.python.org'] -jobs = [Job.spawn_new(geturl, x) for x in urls] - +jobs = [proc.spawn(geturl, x) for x in urls] print 'spawned %s jobs' % len(jobs) -# collect the results from workers, one by one -for url, job in zip(urls, jobs): - print '%s: %s' % (url, repr(job.wait())[:50]) +# collect the results from workers +results = proc.waitall(jobs) +# Note, that any exception in the workers will be reraised by waitall +# unless trap_errors argument specifies otherwise + +for url, result in zip(urls, results): + print '%s: %s' % (url, repr(result)[:50]) diff --git a/examples/twisted_http_proxy.py b/examples/twisted_http_proxy.py index 262f534..fbab912 100644 --- a/examples/twisted_http_proxy.py +++ b/examples/twisted_http_proxy.py @@ -63,5 +63,6 @@ def format_response(response): class MyFactory(Factory): protocol = LineOnlyReceiver +print __doc__ reactor.listenTCP(8888, MyFactory()) reactor.run() diff --git a/examples/twisted_portforward.py b/examples/twisted_portforward.py index 7fe70f7..2474af8 100644 --- a/examples/twisted_portforward.py +++ b/examples/twisted_portforward.py @@ -3,9 +3,9 @@ USAGE: twisted_portforward.py local_port remote_host remote_port""" import sys from twisted.internet import reactor -from eventlet.coros import Job from eventlet.twistedutil import join_reactor from eventlet.twistedutil.protocol import GreenClientCreator, SpawnFactory, UnbufferedTransport +from eventlet import proc def forward(source, dest): try: @@ -22,10 +22,9 @@ def handler(local): client = str(local.getHost()) print 'accepted connection from %s' % client remote = GreenClientCreator(reactor, UnbufferedTransport).connectTCP(remote_host, remote_port) - a = Job.spawn_new(forward, remote, local) - b = Job.spawn_new(forward, local, remote) - a.wait() - b.wait() + a = proc.spawn(forward, remote, local) + b = proc.spawn(forward, local, remote) + proc.waitall([a, b], trap_errors=True) print 'closed connection to %s' % client try: diff --git a/examples/twisted_server.py b/examples/twisted_server.py index c21bd2f..9ae8000 100644 --- a/examples/twisted_server.py +++ b/examples/twisted_server.py @@ -33,6 +33,7 @@ class Chat: finally: self.participants.remove(conn) +print __doc__ chat = Chat() from twisted.internet import reactor reactor.listenTCP(8007, SpawnFactory(chat.handler, LineOnlyReceiverTransport)) diff --git a/examples/twisted_srvconnector.py b/examples/twisted_srvconnector.py index bc171e1..2602baf 100644 --- a/examples/twisted_srvconnector.py +++ b/examples/twisted_srvconnector.py @@ -8,21 +8,16 @@ from eventlet.twistedutil.protocols.basic import LineOnlyReceiverTransport class NoisySRVConnector(SRVConnector): def _ebGotServers(self, failure): - #self.failure = failure return SRVConnector._ebGotServers(self, failure) def pickServer(self): host, port = SRVConnector.pickServer(self) - #if not isinstance(port, int) and self.failure: - # self.failure.raiseException() print 'Resolved _%s._%s.%s --> %s:%s' % (self.service, self.protocol, self.domain, host, port) return host, port -# why TypeError is not raised here? - cred = X509Credentials(None, None) creator = GreenClientCreator(reactor, LineOnlyReceiverTransport) -conn = creator.connectSRV('msrpsx', 'ag-projects.com', +conn = creator.connectSRV('msrps', 'ag-projects.com', connectFuncName='connectTLS', connectFuncArgs=(cred,), ConnectorClass=NoisySRVConnector)