Mais conteúdo relacionado Semelhante a About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014 (20) About Those Python Async Concurrent Frameworks - Fantix @ OSTC 201417. from twisted.internet import protocol, reactor
class Echo(protocol.Protocol):
def dataReceived(self, data):
self.transport.write(data)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
reactor.listenTCP(1234, EchoFactory())
reactor.run()
另外...
19. generator
@defer.inlineCallbacks
def main(endpoint, username="alice", password="secret"):
endpoint = endpoints.clientFromString(reactor, strport)
factory = protocol.Factory()
factory.protocol = imap4.IMAP4Client
try:
endpoint.connect(factory)client = yield
yield client.login(username, password)
yield client.select('INBOX')
info = yield client.fetchEnvelope(imap4.MessageSet(1))
print 'First message subject:', info[1]['ENVELOPE'][1]
except:
print "IMAP4 client interaction failed"
failure.Failure().printTraceback()
task.react(main, sys.argv[1:])
20. greenlet
@defer.inlineCallbacks
def main(endpoint, username="alice", password=“secret”):
endpoint = endpoints.clientFromString(reactor, strport)
factory = protocol.Factory()
factory.protocol = imap4.IMAP4Client
try:
client = yield endpoint.connect(factory)
yield client.login(username, password)
yield client.select('INBOX')
info = yield client.fetchEnvelope(imap4.MessageSet(1))
print 'First message subject:', info[1]['ENVELOPE'][1]
except:
print "IMAP4 client interaction failed"
failure.Failure().printTraceback()
task.react(main, sys.argv[1:])
def main(endpoint, username="alice", password=“secret”):
endpoint = endpoints.clientFromString(reactor, strport)
factory = protocol.Factory()
factory.protocol = imap4.IMAP4Client
try:
client = endpoint.connect(factory)
client.login(username, password)
client.select('INBOX')
info = client.fetchEnvelope(imap4.MessageSet(1))
print 'First message subject:', info[1]['ENVELOPE'][1]
except:
print "IMAP4 client interaction failed"
failure.Failure().printTraceback()
task.react(main, sys.argv[1:])
21. Eventlet / Gevent
from gevent import monkey
monkey.patch_all()
def handle(socket, address):
url = socket.recv()
data = urllib.urlopen(url).read()
socket.send(data)
server = StreamServer(('127.0.0.1', 1234), handle)
server.serve_forever()
24. activity in day * 30
issues mails commits
Twisted 122 40 19 (258)
Tornado 31 36 17
Gevent 12 5 26
2014.03.18
32. protocol && transport
class MyServerUdpEchoProtocol:
def connection_made(self, transport):
print('start', transport)
self.transport = transport
def datagram_received(self, data, addr):
print('Data received:', data, addr)
self.transport.sendto(data, addr)
def error_received(self, exc):
print('Error received:', exc)
def connection_lost(self, exc):
print('stop', exc)
33. Future
In [1]: from asyncio.futures import Future
In [2]: import asyncio
In [3]: loop = asyncio.get_event_loop()
In [4]: f = Future()
In [5]: loop.call_later(2, f.set_result, 'OK')
Out[5]: TimerHandle(74709.18277206, <bound method
Future.set_result of Future<PENDING>>, ('OK',))
In [6]: loop.run_until_complete(f)
Out[6]: 'OK'
34. Task && Coroutine
import asyncio
from asyncio.tasks import Task
@asyncio.coroutine
def greet_every_two_seconds():
while True:
print('Hello World')
yield from asyncio.sleep(2)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
t = Task(greet_every_two_seconds())
loop.run_until_complete(t)