asynchronous python a gentle introduction

26
…….Asynchronous Python: A Gentle Introduction……… James Cropcho, PyData New York 2017

Upload: pydata

Post on 16-Mar-2018

125 views

Category:

Technology


17 download

TRANSCRIPT

Page 1: Asynchronous Python A Gentle Introduction

…….Asynchronous Python: A Gentle Introduction………

James Cropcho, PyData New York 2017

Page 2: Asynchronous Python A Gentle Introduction

Structure of Presentation(1) Define asynchronous programming, thereby disambiguating related but

independent concepts(2) Define coroutines(3) Survey Python asynchronous programming implementations(4) First steps towards asynchrony for a program(5) Some asyncio tips

I target CPython 3.7 only, and ignore deprecated interfaces.

I’m no longer comparing implementations using different libraries, as I’d planned in the talk proposal.

Please do not hold questions until the end.

Page 3: Asynchronous Python A Gentle Introduction

You don’t need me around to look up the exact syntax for what you’re trying to do via StackOverflow; I am here to get you to that point, where you are putting down

brass tacks.

I also aim to provide “evergreen” material which does not become irrelevant rapidly.

Page 4: Asynchronous Python A Gentle Introduction

Processes and ThreadsShared memory: Threads, yes. Processes, no.

“The principal challenge of multi-threaded applications is coordinating threads that share data or other resources. To that end, the threading module provides a number of synchronization primitives including locks, events, condition variables, and semaphores. …While those tools are powerful, minor design errors can result in problems that are difficult to reproduce.” (Multi-threading)

Processes: interprocess communication

Page 5: Asynchronous Python A Gentle Introduction

Concurrent Computing vs. Parallel Computing“Parallel computing is closely related to concurrent computing—they are frequently used together, and often conflated, though the two are distinct: it is possible to have parallelism without concurrency (such as bit-level parallelism), and concurrency without parallelism (such as multitasking by time-sharing on a single-core CPU).[5][6]” (Wikipedia: Parallel Computing)

Page 6: Asynchronous Python A Gentle Introduction

Preemptive vs. Cooperative MultitaskingWith cooperative multitasking, the code “volunteers” to give up control.

Page 7: Asynchronous Python A Gentle Introduction

Blocking vs non-blockingBlocking: “non-asynchronous”

“Can I do something else while I’m waiting?

Page 8: Asynchronous Python A Gentle Introduction

So What is Asynchrony?Generally regarded as a single-threaded programming “style”/”paradigm” with a queue of non-blocking tasks.

One thing at a time!

Baking bread while doing dishes (state is retained across return to a previous context)

So, as commonly stated, is there a speed boost from this?

Page 9: Asynchronous Python A Gentle Introduction

@sixty_north

Coroutine Concurrency in Python 3Getting to grips with asyncio

Robert Smallshire@robsmallshire

1

Page 10: Asynchronous Python A Gentle Introduction

Definitions #1Dealing with multiple things at once versus doing multiple things at once.

Concurrency Parallelism

Tasks start, run, and complete in

overlapping time periods

Tasks run simultaneously

coroutines10

threads / processes

+ multicore

Page 11: Asynchronous Python A Gentle Introduction

Definitions #2The definition of synchronous contradicts common usage

Sequential (Synchronous)

Must complete before proceeding

Asynchronous

No need to wait before

proceeding

overall duration shorter

11

overall duration longer

Page 12: Asynchronous Python A Gentle Introduction

Definitions #4

Coöperative multitasking

Tasks yield to scheduler

Preëmptive multitasking

Scheduler interrupts tasks

Inconvenient context switches 1

2

Uncooperative tasks hang system

Page 13: Asynchronous Python A Gentle Introduction

Coroutines“Coroutines are computer-program components that generalize subroutines for non-preemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations” (Wikipedia: Coroutines)

Subroutines are a subset of coroutines. Generators (semicoroutines) are between the two.

Tasklets, generators, async/await vs generator syntax, greenlets…

Coroutine vs coroutine objects (Smallshire): code only, callable vs code+state, awaitable

Page 14: Asynchronous Python A Gentle Introduction

Coroutines 2Things a coroutine can do (18.5 asyncio):

● result = await future or result = yield from future

● result = await coroutine or result = yield from coroutine

● return expression

● raise exception

Page 15: Asynchronous Python A Gentle Introduction

15

Filter and print coroutineSimilar to async_search, but prints all matches

def async_print_matches(iterable,

for item in iterable:

if predicate(item):

predicate):

print("Found :",item,

yield

end=",

")

Page 16: Asynchronous Python A Gentle Introduction

Event Loops1. while 1 (Maxwell):2. wait for something to happen3. react to whatever happened

…and Executors: Executors are great for if one must really use a synch/blocking library (Langa)

Task: making a coroutine into a future, and enqueueing it upon the event loop (asyncio-speak only)

Page 17: Asynchronous Python A Gentle Introduction

I/O aware scheduler

Tasks suspend when waiting, scheduled when data ready

12

3

45

6

7

8waiting on I/O

I/O ready

scheduled

17

Page 18: Asynchronous Python A Gentle Introduction

Round-robin schedulerTasks run in circular order

12

3

45

6

7

8

18

Page 19: Asynchronous Python A Gentle Introduction

Why is This Difficult?“The principal challenge of multi-threaded applications is coordinating threads that share data or other resources.” (11.4 Multi-threading)

Subtler feedback/failures

Simultaneously concerned with different abstraction/interface/implementation levels, i.e. concerned with implementation in the same space/code as interface AND: this is because it’s cooperative multitasking OR: concerned with stuff going on outside of the thread/context

Page 20: Asynchronous Python A Gentle Introduction

First steps towards asynchrony for a programScaling: “Processes: tens, Threads: hundreds, Async: thousands” (Grinberg)

First ask: is there already an asynchronous package to do this precise thing? aio-libs is very helpful for this!

Regardless, asyncio is probably your best bet.

Page 21: Asynchronous Python A Gentle Introduction

Python Asynchronous Implementationsasycio is a massive framework encompassing high and low abstraction APIs. Much other tooling is built atop it.

Stackless Python: allows “tasklets” which are like coroutines/coroutine-futures

Both Eventlet and Gevent use Greenlet. Greenlet is a spin-off of Stackless. “Greenlets are provided as a C extension module for the regular unmodified interpreter.” A “greenlet” is a coroutine. (Greenlet GH)

“gevent is inspired by eventlet” (Gevent site)

Curio and Trio (new kids on the block), but worth knowing of

Page 22: Asynchronous Python A Gentle Introduction

asyncio TipsPYTHONASYNCIODEBUG=1 python -Wdefault groovy-threads.py

“Long CPU-intensive tasks must routinely release the CPU to avoid starving other

tasks. This can be done by “sleeping” periodically, such as once per loop iteration.

To tell the loop to return control back as soon as possible, sleep for 0 seconds.

Example: await asyncio.sleep(0)” (Grinberg) NOTE: When is this less

important?

Blocking library functions are incompatible with async frameworks (Grinberg)

socket.*, select.* subprocess.*, os.waitpid threading.*,

multiprocessing.* time.sleep

Page 23: Asynchronous Python A Gentle Introduction

Further Learning: refer to ‘Works Cited’

Page 24: Asynchronous Python A Gentle Introduction

Works [Heavily] Cited and This Work’s License (1) https://commons.wikimedia.org/wiki/File:Open_Make_Up_For_Ever_2013_-_

Team_-_France_-_15.jpg(2) Robert Smallshire (2017) , Getting To Grips With asyncio. Sixty North(3) Demystifying async, await, and asyncio by Sebastiaan Mathôt(4) [18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks]

https://docs.python.org/3.7/library/asyncio.html(5) [11.4. Multi-threading]

https://docs.python.org/3.7/tutorial/stdlib2.html#multi-threading(6) Miguel Grinberg, Asynchronous Python for the Complete Beginner

Distributed under the Creative Commons Attribution-ShareAlike 4.0 International license, save for Smallshire’s slides.

Page 25: Asynchronous Python A Gentle Introduction

Works [Heavily] Cited 2(1) Łukasz Langa - Thinking In Coroutines - PyCon 2016(2) [Maxwell]

https://www.quora.com/How-does-an-event-loop-work/answer/Timothy-Maxwell

Distributed under the Creative Commons Attribution-ShareAlike 4.0 International license, save for Smallshire’s slides.

Page 26: Asynchronous Python A Gentle Introduction

Question and Answer Period

Also…I am seeking pan-industry Python contractor work

and a job in finance.

Got anything for me?