Concurrency in Python4k
-
Upload
rhcarvalho -
Category
Software
-
view
176 -
download
0
description
Transcript of Concurrency in Python4k
![Page 1: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/1.jpg)
Concurrency in Python 4k
PyWAW #42
17 Nov 2014Rodolfo Carvalho
Lead Developer @ Base
![Page 2: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/2.jpg)
#42
![Page 3: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/3.jpg)
![Page 4: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/4.jpg)
![Page 5: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/5.jpg)
complex made easy
![Page 6: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/6.jpg)
focus
![Page 7: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/7.jpg)
Concurrency in Py4k
![Page 8: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/8.jpg)
concurrency
![Page 9: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/9.jpg)
![Page 10: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/10.jpg)
![Page 11: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/11.jpg)
old tools
![Page 12: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/12.jpg)
threading
multiprocessing
Coroutines via Enhanced Generators (PEP 342, Python 2.5)
![Page 13: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/13.jpg)
Python 3
![Page 14: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/14.jpg)
all from Python 2.x
concurrent.futures
!
asyncio (Python 3.4)
![Page 15: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/15.jpg)
![Page 16: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/16.jpg)
structure
![Page 17: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/17.jpg)
Tim
eWork done
main
func A
func B
func C
func D
![Page 18: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/18.jpg)
CSP model
![Page 19: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/19.jpg)
Tim
eWork done
Process A Process B
Channel
![Page 20: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/20.jpg)
concurrency primitives
goroutines
channels
select
![Page 21: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/21.jpg)
goroutines
func MightTakeLong() { // ...} MightTakeLong() go MightTakeLong()
![Page 22: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/24.jpg)
potentially real example
![Page 25: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/27.jpg)
Python?
Python 2.7: geventGreenlets + Queues
Python 3.4: asyncio Coroutines + Queues
PythonCSP
![Page 28: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/28.jpg)
Other languages
occam
Limbo
Clojure
![Page 29: Concurrency in Python4k](https://reader033.fdocuments.in/reader033/viewer/2022052623/559e53cc1a28ab52448b47cd/html5/thumbnails/29.jpg)
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