import asyncio

import asyncio

Brněnské Pyvi, 2014-01-30

870d613430249e453343efc9667ef636?s=128

Petr Viktorin

January 30, 2014
Tweet

Transcript

  1. import asyncio Petr Viktorin encukou@gmail.com Pyvo, 2014-01-30

  2. The problem Do something useful while waiting

  3. The problem Do something useful while waiting (for IO)

  4. The problems Do something useful while waiting (for IO) Do

    several things at the same time
  5. Multiple processes import subprocess

  6. Threads import threading

  7. Greenlets Event loop Cooperative multitacking Monkeypatching pip install eventlet gevent

  8. Callbacks Explicit cooperative multitasking pip install twisted

  9. Help! Which do I use?

  10. Compare: web frameworks

  11. Compare: web frameworks Flask app

  12. Compare: web frameworks Flask app Pyramid debug toolbar

  13. Compare: web frameworks Flask app Pyramid debug toolbar Cherrypy logging

  14. Compare: web frameworks Flask app Pyramid debug toolbar Cherrypy logging

    PEP 333 - Python Web Server Gateway Interface
  15. Let's do the same for async!

  16. Let's do the same for async! import asyncio

  17. Let's do the same for async! import asyncio Python 3.4+

  18. Let's do the same for async! import asyncio Python 3.4+

    pip install tulip
  19. Let's do the same for async! import asyncio Python 3.4+

    pip install tulip Python 3.3+
  20. asyncio Explicit cooperative multitasking

  21. asyncio Explicit cooperative multitasking like Twisted

  22. Global event loop

  23. Global event loop Event loop policy

  24. Callbacks

  25. Callbacks call_soon(callback, args)

  26. Callbacks call_soon(callback, args) call_later(delay, cb, *args)

  27. Callbacks call_soon(callback, args) call_later(delay, cb, *args) What is time?

  28. Callback style import asyncio def print_and_repeat(loop): print('Hello World') loop.call_later(2, print_and_repeat,

    loop) loop = asyncio.get_event_loop() loop.call_soon(print_and_repeat, loop) loop.run_forever()
  29. Futures

  30. Futures Placeholders for a result

  31. Futures Placeholders for a result future.result()

  32. Futures Placeholders for a result future.result() future.set_result()

  33. Futures Placeholders for a result future.result() future.set_result() future.set_exception()

  34. Futures Placeholders for a result future.result() future.set_result() future.set_exception() future.add_done_callback()

  35. Futures Placeholders for a result future.result() future.set_result() future.set_exception() future.add_done_callback() concurrent.futures

  36. Locks, Semaphores, Queues

  37. Locks, Semaphores, Queues http://xkcd.com/927/

  38. Couroutines & Tasks import asyncio @asyncio.coroutine def greet_every_two_seconds(): while True:

    print('Hello World') yield from asyncio.sleep(1) print('How are you?') yield from asyncio.sleep(1) loop = asyncio.get_event_loop() loop.run_until_complete(greet_every_two_seconds())
  39. Transports & Protocols Transport: TCP, UDP, SSL Protocol: HTTP, FTP,

    IRC
  40. Echo client import asyncio class EchoClient(asyncio.Protocol): message = 'This is

    the message. It will be echoed.' def connection_made(self, transport): transport.write(self.message.encode()) print('data sent: {}'.format(self.message)) def data_received(self, data): print('data received: {}'.format(data.decode())) def connection_lost(self, exc): print('server closed the connection') asyncio.get_event_loop().stop() loop = asyncio.get_event_loop() coro = loop.create_connection(EchoClient, '127.0.0.1', 8888) loop.run_until_complete(coro) loop.run_forever() loop.close()
  41. Echo server import asyncio class EchoServer(asyncio.Protocol): def connection_made(self, transport): peername

    = transport.get_extra_info('peername') print('connection from {}'.format(peername)) self.transport = transport def data_received(self, data): print('data received: {}'.format(data.decode())) self.transport.write(data) # close the socket self.transport.close() loop = asyncio.get_event_loop() coro = loop.create_server(EchoServer, '127.0.0.1', 8888) server = loop.run_until_complete(coro) print('serving on {}'.format(server.sockets[0].getsockname())) try: loop.run_forever() except KeyboardInterrupt: print("exit") finally: server.close() loop.close()
  42. ? Petr Viktorin @encukou.cz @encukou encukou@gmail.com github.com/encukou Slides available under

    the Creative Commons Attribution-ShareAlike 3.0 http://creativecommons.org/licenses/by-sa/3.0/
  43. Sources & links PEP 3156 - Asynchronous IO Support Rebooted:

    the "asyncio" Module PEP 3148 - futures - execute computations asynchronously PEP 3153 (Superseded) - Asynchronous IO support http://docs.python.org/3.4/library/asyncio.html Youtube: Guido van Rossum - Tulip: Async I/O for Python 3