Floris Bruynooghe
email: flub@devork.be
twitter: @flubdevork
irc: flub
Me:
sock = socket.socket() sock.connect(('localhost', 7)) sock.send('Hello world') # <- blocks sock.recv(4096) # <- blocks sock.close()
srv_sock = socket.socket() srv_sock.bind(('', 7)) srv_sock.listen(5) while True: client, addr = srv_sock.accept() # <- blocks data = client.recv(4096) # <- blocks while data: client.send(data) # <- blocks data = client.recv(4096) # <- blocks client.close()
srv_sock = socket.socket() srv_sock.bind(('', 7)) srv_sock.listen(5) while True: client, addr = srv_sock.accept() t = threading.Thread(target=handle_client, args=client) t.start() def handle_client(sock): data = sock.recv(4096) while data: sock.send(data) data = sock.recv(4096) sock.close()
srv_sock = socket.socket() srv_sock.bind(('', 7)) srv_sock.listen(5) rlist = [srv_sock] wlist = clients = [] while True: readers, writers, _ = select.select(rlist, wlist, []) for client in clients: if client in readers and client in writers: data = client.recv(4096) if data == b'': clients.remove(client) else: client.send(data) if srv_sock in readers: clients.append(srv_sock.accept()[0]) rlist = [srv_sock] + clients
srv_sock = eventlet.green.socket.socket() srv_sock.bind(('', 7)) srv_sock.listen(5) while True: client, addr = srv_sock.accept() # <- yield eventlet.spawn_n(handle_client, client) def handle_client(sock): data = sock.recv(4096) # <- yield while data: sock.send(data) # <- yield data = sock.re sock.close()
main = greenlet.getcurrent() l = [] def func(other): l.append(0) other.switch() l.append(2) g = greenlet.greenlet(run=func) g.switch(main) l.append(1) g.switch() assert l == [0, 1, 2] assert g.dead
def fun(): eventlet.sleep(5) return 42 g = eventlet.spawn(fun) assert bool(g) is False eventlet.sleep(0) # yield assert bool(g) is True assert g.dead is False rv = g.wait() assert rv == 42
Concurrency, but not too much
srv_sock = eventlet.green.socket.socket() srv_sock.bind(('', 7)) srv_sock.listen(5) pool = eventlet.GreenPool(1000) while True: client = srv_sock.accept() pool.spawn_n(handle_client, client) def handle_client(sock): ...
eventlet.monkey_patch() srv_sock = socket.socket() srv_sock.bind(('', 7)) srv_sock.listen(5) while True: client = srv_sock.accept() t = threading.Thread(target=handle_client, args=client) t.start() def handle_client(sock): data = sock.recv(4096) while data: sock.send(data) data = sock.recv(4096) sock.close()
ai = eventlet.tpool.execute( socket.getaddrinfo, 'www.kame.net', None, socket.AF_INET6)
Real threads:
Green threads:
Using multiple threads:
q = eventlet.xthread.Queue() def producer(): q.put(42) def consumer(): return p.get() t = threading.Thread(target=producer) g = eventlet.spawn(consumer) t.start() assert g.wait() == 42
Blocking:
Unblocking:
Blocking:
Unblocking:
(Q4 2013)
In Summary:
Questions?
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |