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

Asynchronous Programming in python: How and when to use it

Issa Jubril
September 16, 2017

Asynchronous Programming in python: How and when to use it

My talk at pycon Nigeria 2017

Issa Jubril

September 16, 2017
Tweet

Other Decks in Programming

Transcript

  1. Asynchronous Programming in python: How and when to use it

    Jubril Issa Software Developer @ Andela
  2. Once Upon a time…. endpoints = [list of endpoints] for

    i in endpoints: resp = requests.get(i) do_stuff(resp)
  3. Synchronous Programming Synchronous programming means that, barring conditionals and function

    calls, code is executed sequentially from top-to-bottom, blocking on long-running tasks such as network requests and disk I/O.
  4. What then is asynchronous programming Asynchronous programming means that the

    engine runs in an event loop. When a blocking operation is needed, the request is started, and the code keeps running without blocking for the result. When the response is ready, an interrupt is fired, which causes an event handler to be run, where the control flow continues. In this way, a single program thread can handle many concurrent operations.
  5. Real World Analogy: Synchronous There are 500 people on the

    queue - Attendant takes 2 minute to sell fuel and 1 minute to collect payment - Attendant takes a total of 3 minute to attend to each customer - In 1 hour 20 people would be attended to - For 500 people the attendant would take 25 hours
  6. Real World Analogy: Asynchronous There are 500 people on the

    queue - Attendant takes 2 minute to sell fuel - Attendant moves to attend to the next customer while the previous customer gets the cash ready and hands over the cash to the attendant while selling for the next customer - Attendant takes a total of 3 minute to attend to each customer - In 1 hour 30 people would be attended to - For 500 people the attendant would around 16.6 hours
  7. Asynchronous programming in python There are lot of options of

    asynchronous programming in python - Asyncio (Added in python 3.4) - Twisted - Gevent - Tornado - Curio - Eventlet - etc
  8. Asyncio From python doc: This module provides infrastructure for writing

    single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives.
  9. Nice thing about asyncio - It uses an event loop,

    so we don’t need to worry about multithreading (GIL Issue, context switching, race conditions, resource starvation) - It runs code concurrently (It’s a form of programming which means doing many things at once) - It takes advantage of the system resources
  10. Async and Await - Added as part of the standard

    library in python 3.5 + - Makes it more clear that your code is asynchronous (easier to think about than threads) - Better than callback and promises - The async keyword goes before def to show that a method is asynchronous. - The await keyword replaces yield from and makes it more clear that you are waiting for a coroutine to finish.