29
Concurrency in Python 4k PyWAW #42 17 Nov 2014 Rodolfo Carvalho Lead Developer @ Base

Concurrency in Python4k

Embed Size (px)

DESCRIPTION

This talk was presented in the PyWaw #42 meeting in Warsaw, Poland. It's about concurrency as a pattern for structuring your code. Here, Python 4k means actually the Go language, which has interesting concurrency features built into the language. I compare those primitives from Communicating Sequential Processes (CSP) model, readily available in Go, with alternatives you can use to write CSP-like code in Python.

Citation preview

Page 1: Concurrency in Python4k

Concurrency in Python 4k

PyWAW #42

17 Nov 2014Rodolfo Carvalho

Lead Developer @ Base

Page 2: Concurrency in Python4k

#42

Page 3: Concurrency in Python4k
Page 4: Concurrency in Python4k
Page 5: Concurrency in Python4k

complex made easy

Page 6: Concurrency in Python4k

focus

Page 7: Concurrency in Python4k

Concurrency in Py4k

Page 8: Concurrency in Python4k

concurrency

Page 9: Concurrency in Python4k
Page 10: Concurrency in Python4k
Page 11: Concurrency in Python4k

old tools

Page 12: Concurrency in Python4k

threading

multiprocessing

Coroutines via Enhanced Generators (PEP 342, Python 2.5)

Page 13: Concurrency in Python4k

Python 3

Page 14: Concurrency in Python4k

all from Python 2.x

concurrent.futures

!

asyncio (Python 3.4)

Page 15: Concurrency in Python4k
Page 16: Concurrency in Python4k

structure

Page 17: Concurrency in Python4k

Tim

eWork done

main

func A

func B

func C

func D

Page 18: Concurrency in Python4k

CSP model

Page 19: Concurrency in Python4k

Tim

eWork done

Process A Process B

Channel

Page 20: Concurrency in Python4k

concurrency primitives

goroutines

channels

select

Page 21: Concurrency in Python4k

goroutines

func MightTakeLong() { // ...} MightTakeLong() go MightTakeLong()

Page 22: Concurrency in Python4k

channels// Declaring and initializingc := make(chan int) // Sending on a channelc <- 1 // Receiving from a channel// The "arrow" indicates the direction of data flowvalue = <-c

Page 23: Concurrency in Python4k

selectfunc fibonacci(c, quit chan int) { x, y := 0, 1 for { select { case c <- x: x, y = y, x+y case <-quit: fmt.Println("quit") return } }}

func main() { c := make(chan int) quit := make(chan int) go func() { for i := 0; i < 10; i++ { fmt.Println(<-c) } quit <- 0 }() fibonacci(c, quit)}

Page 24: Concurrency in Python4k

potentially real example

Page 25: Concurrency in Python4k

Concurrent search

func Google(query string) (results []Result) { c := make(chan Result) go func() { c <- Web(query) } () go func() { c <- Image(query) } () go func() { c <- Video(query) } () for i := 0; i < 3; i++ { result := <-c results = append(results, result) } return}

Page 26: Concurrency in Python4k

timeoutc := make(chan Result) go func() { c <- Web(query) } () go func() { c <- Image(query) } () go func() { c <- Video(query) } () timeout := time.After(80 * time.Millisecond) for i := 0; i < 3; i++ { select { case result := <-c: results = append(results, result) case <-timeout: fmt.Println("timed out") return }} return

Page 27: Concurrency in Python4k

Python?

Python 2.7: geventGreenlets + Queues

Python 3.4: asyncio Coroutines + Queues

PythonCSP

Page 28: Concurrency in Python4k

Other languages

occam

Limbo

Clojure

Page 29: Concurrency in Python4k

1. rodolfocarvalho.net!

2. golang.org/s/concurrency-is-not-parallelism!

3. golang.org/s/concurrency-patterns!

4. www.infoq.com/presentations/core-async-clojure!

5. asyncio.org!

6. wiki.python.org/moin/Concurrency