Upgrade to Pro — share decks privately, control downloads, hide ads and more …

import asyncio

import asyncio

Brněnské Pyvi, 2014-01-30

Petr Viktorin

January 30, 2014
Tweet

More Decks by Petr Viktorin

Other Decks in Programming

Transcript

  1. import asyncio
    Petr Viktorin
    [email protected]
    Pyvo, 2014-01-30

    View Slide

  2. The problem
    Do something useful
    while waiting

    View Slide

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

    View Slide

  4. The problems
    Do something useful
    while waiting
    (for IO)
    Do several things
    at the same time

    View Slide

  5. Multiple processes
    import subprocess

    View Slide

  6. Threads
    import threading

    View Slide

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

    View Slide

  8. Callbacks
    Explicit cooperative
    multitasking
    pip install twisted

    View Slide

  9. Help! Which do I use?

    View Slide

  10. Compare: web frameworks

    View Slide

  11. Compare: web frameworks
    Flask app

    View Slide

  12. Compare: web frameworks
    Flask app
    Pyramid debug toolbar

    View Slide

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

    View Slide

  14. Compare: web frameworks
    Flask app
    Pyramid debug toolbar
    Cherrypy logging
    PEP 333 - Python Web Server
    Gateway Interface

    View Slide

  15. Let's do the same for async!

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. asyncio
    Explicit cooperative
    multitasking

    View Slide

  21. asyncio
    Explicit cooperative
    multitasking
    like Twisted

    View Slide

  22. Global event loop

    View Slide

  23. Global event loop
    Event loop policy

    View Slide

  24. Callbacks

    View Slide

  25. Callbacks
    call_soon(callback, args)

    View Slide

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

    View Slide

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

    View Slide

  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()

    View Slide

  29. Futures

    View Slide

  30. Futures
    Placeholders for a result

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  36. Locks, Semaphores, Queues

    View Slide

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

    View Slide

  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())

    View Slide

  39. Transports & Protocols
    Transport: TCP, UDP, SSL
    Protocol: HTTP, FTP, IRC

    View Slide

  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()

    View Slide

  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()

    View Slide

  42. ?
    Petr Viktorin
    @encukou.cz
    @encukou
    [email protected]
    github.com/encukou
    Slides available under the
    Creative Commons Attribution-ShareAlike 3.0
    http://creativecommons.org/licenses/by-sa/3.0/

    View Slide

  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

    View Slide