diff options
| author | Ryan Williams <rdw@lindenlab.com> | 2009-12-31 22:30:08 -0800 |
|---|---|---|
| committer | Ryan Williams <rdw@lindenlab.com> | 2009-12-31 22:30:08 -0800 |
| commit | 4aa200caee92cb5839de680552d2133bdb002f21 (patch) | |
| tree | 4cf3cbeb0ddcb86244d4cda3e541c76194ea4871 /examples | |
| parent | 3ddbba23de2ad1485d968bddbbb3e2866b4429c2 (diff) | |
| download | eventlet-4aa200caee92cb5839de680552d2133bdb002f21.tar.gz | |
Documentation sweep -- slightly improved documentation for a bunch of things, and beefed up the examples to keep up with the parlance of our times.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/echoserver.py | 20 | ||||
| -rw-r--r-- | examples/webcrawler.py | 41 |
2 files changed, 31 insertions, 30 deletions
diff --git a/examples/echoserver.py b/examples/echoserver.py index 6e12368..0e1f482 100644 --- a/examples/echoserver.py +++ b/examples/echoserver.py @@ -10,24 +10,30 @@ You terminate your connection by terminating telnet (typically Ctrl-] and then 'quit') """ -from eventlet import api +import eventlet +from eventlet.green import socket -def handle_socket(reader, writer): +def handle(reader, writer): print "client connected" while True: # pass through every non-eof line x = reader.readline() if not x: break writer.write(x) - print "echoed", x + writer.flush() + print "echoed", x, print "client disconnected" print "server socket listening on port 6000" -server = api.tcp_listener(('0.0.0.0', 6000)) +server = socket.socket() +server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1) +server.bind(('0.0.0.0', 6000)) +server.listen(50) +pool = eventlet.GreenPool(10000) while True: try: new_sock, address = server.accept() - except KeyboardInterrupt: + print "accepted", address + pool.spawn_n(handle, new_sock.makefile('r'), new_sock.makefile('w')) + except (SystemExit, KeyboardInterrupt): break - # handle every new connection with a new coroutine - api.spawn(handle_socket, new_sock.makefile('r'), new_sock.makefile('w')) diff --git a/examples/webcrawler.py b/examples/webcrawler.py index 2dfbd45..53c6e8f 100644 --- a/examples/webcrawler.py +++ b/examples/webcrawler.py @@ -2,32 +2,27 @@ """\ @file webcrawler.py -This is a simple web "crawler" that fetches a bunch of urls using a coroutine pool. It fetches as - many urls at time as coroutines in the pool. +This is a simple web "crawler" that fetches a bunch of urls using a pool to +control the number of outbound connections. It has as many simultaneously open +connections as coroutines in the pool. + +The prints in the body of the fetch function are there to demonstrate that the +requests are truly made in parallel. """ urls = ["http://www.google.com/intl/en_ALL/images/logo.gif", - "http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif", - "http://eventlet.net"] + "https://wiki.secondlife.com/w/images/secondlife.jpg", + "http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif"] -import time -from eventlet.green import urllib2 -from eventlet import coros +import eventlet +from eventlet.green import urllib2 def fetch(url): - # we could do something interesting with the result, but this is - # example code, so we'll just report that we did it - print "%s fetching %s" % (time.asctime(), url) - req = urllib2.urlopen(url) - print "%s fetched %s (%s)" % (time.asctime(), url, len(req.read())) - -pool = coros.CoroutinePool(max_size=4) -waiters = [] -for url in urls: - waiters.append(pool.execute(fetch, url)) - -# wait for all the coroutines to come back before exiting the process -for waiter in waiters: - waiter.wait() - - + print "opening", url + body = urllib2.urlopen(url).read() + print "done with", url + return url, body + +pool = eventlet.GreenPool(200) +for url, body in pool.imap(fetch, urls): + print "got body from", url, "of length", len(body)
\ No newline at end of file |
