Slide 1

Slide 1 text

1 PyCon ZA 2020 Has Asyncio Solved Concurrency?

Slide 2

Slide 2 text

2 ​ Jacob Unna ​ Software Engineer @ Deloitte Analytics & Cognitive ​ [email protected] @JacobUnna

Slide 3

Slide 3 text

3 What is Concurrency?

Slide 4

Slide 4 text

4 Concurrency in Python − Threads − Asyncio

Slide 5

Slide 5 text

5 Concurrency in Python − Threads − Asyncio − How do they work? − Which is better? − Does asyncio spell the end for threads?

Slide 6

Slide 6 text

6 Scheduling

Slide 7

Slide 7 text

7 Scheduling

Slide 8

Slide 8 text

8

Slide 9

Slide 9 text

9 Cooperative Scheduling Asyncio

Slide 10

Slide 10 text

10 Cooperative scheduling • Can be implemented at the application level • Everything must be non-blocking

Slide 11

Slide 11 text

11 Cooperative scheduling • Can be implemented at the application level • Everything must be non-blocking Cooperation

Slide 12

Slide 12 text

12 Preemptive Scheduling Threads

Slide 13

Slide 13 text

13 Preemptive scheduling • Run any two bits of code at the same time • Handled by the operating system − Wouldn't be possible using pure Python − Operating system preempts running code i.e. freezes it mid-flow and switches the CPU's attention to something else • Doesn't get stuck on uncooperative code

Slide 14

Slide 14 text

14 Today's Agenda − How do threads and asyncio work? − Which is better? − Does asyncio spell the end for threads?

Slide 15

Slide 15 text

15 Which is better? Threads Asyncio Overhead when switching task High Low

Slide 16

Slide 16 text

16 Which is better? Threads Asyncio Overhead when switching task High Low Easy to reason about No Yes

Slide 17

Slide 17 text

17 Which is better? Threads Asyncio Overhead when switching task High Low Easy to reason about No Yes Handle blocking code Yes No

Slide 18

Slide 18 text

18 Which is better? Threads Asyncio Overhead when switching task High Low Easy to reason about No Yes Handle blocking code Yes No

Slide 19

Slide 19 text

19 Today's Agenda − How do threads and asyncio work? − Which is better? − Does asyncio spell the end for threads?

Slide 20

Slide 20 text

20 Which is better? • Cheap • Safe • Easy to use • Can cut through solid wood

Slide 21

Slide 21 text

21 Can I do this with asyncio? Decision flow Use asyncio Use threads Yes No

Slide 22

Slide 22 text

22 ​ Asyncio ≠ Cooperative Scheduling

Slide 23

Slide 23 text

23 ​ Because cooperative scheduling takes place at the application level, it is easy to create new frameworks

Slide 24

Slide 24 text

24 ​ Because cooperative scheduling takes place at the application level, it is easy to create new frameworks ​ (Relative to creating a new operating system)

Slide 25

Slide 25 text

25 Asyncio

Slide 26

Slide 26 text

26 Gevent Tornado uvloop Twisted Asyncio PyZMQ Curio Trio Diesel

Slide 27

Slide 27 text

27 Gevent Tornado uvloop Twisted Asyncio PyZMQ Curio Trio Diesel

Slide 28

Slide 28 text

28 ​ Questions? ​ Jacob Unna ​ [email protected] @JacobUnna Code: git.io/JU6FT