+ All Categories
Home > Documents > Message-passing concurrency in Python

Message-passing concurrency in Python

Date post: 14-Sep-2014
Category:
View: 1,241 times
Download: 0 times
Share this document with a friend
Description:
Concurrency and parallelism in Python are always hot topics. This talk will look the variety of forms of concurrency and parallelism. In particular this talk will give an overview of various forms of message-passing concurrency which have become popular in languages like Scala and Go. A Python library called python-csp which implements similar ideas in a Pythonic way will be introduced and we will look at how this style of programming can be used to avoid deadlocks, race hazards and "callback hell".
Popular Tags:
27
Message-passing concurrency in Python Sarah Mount – @snim2 EuroPython 2014 Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 1 / 27
Transcript
Page 1: Message-passing concurrency in Python

Message-passing concurrency in Python

Sarah Mount – @snim2

EuroPython 2014

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 1 / 27

Page 2: Message-passing concurrency in Python

1 Why multicore?

2 Message passing: an idea whose time has come (back)

3 Message passing: all the cool kids are doing it

4 Design choices in message passing languages and libraries

5 Message passing in Python too!

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 2 / 27

Page 3: Message-passing concurrency in Python

Section 1

Why multicore?

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 3 / 27

Page 4: Message-passing concurrency in Python

Why multicore? (1/2)

Feature size (mm) vs time (year). CPU sizes are shrinking! Get the rawdata here: http://cpudb.stanford.edu/

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 4 / 27

Page 5: Message-passing concurrency in Python

Why multicore? (2/2)

New platforms, such as this board from Adapteva Inc. which reportedlyruns at 50GFPLOPS/Watt. Photo c©Adapteva Inc.

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 5 / 27

Page 6: Message-passing concurrency in Python

Section 2

Message passing: an idea whose time has come (back)

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 6 / 27

Page 7: Message-passing concurrency in Python

Message passing concurrency

Tony Hoare

CSPa (Communicating Sequential Processes) was invented by Tony Hoarein the late 70s / early 80s as a response to the difficulty of reasoningabout concurrent and parallel code. It was implemented as the OCCAMprogramming language for the INMOS Transputer.

aother process algebras are availableSarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 7 / 27

Page 8: Message-passing concurrency in Python

Message passing concurrency

CSP1 take the view that computation consists of communicating processeswhich are individually made up of sequential instructions. Thecommunicating processes all run ”at the same time” and share no data.Because they do not share data they have to cooperate by synchronisingon events. A common form of synchronisation (but not the only one) is topass data between processes via channels.

THINK UNIX processes communicating via pipes.

BEWARE CSP is an abstract ideas which use overloaded jargon, aprocess or an event need not be reified as an OS process or GUI event. . .

1other process algebras are availableSarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 8 / 27

Page 9: Message-passing concurrency in Python

Benefits of message passing

Threads and locking are tough! Deadlocks, starvation, race hazards aretough. Locks do not compose easily, errors in locking cannot easily berecovered from, finding the right level of granularity is hard.

CSP does not exhibit race hazards, there are no locks, the hairy details areall hidden away in either a language runtime or a library. WIN!

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 9 / 27

Page 10: Message-passing concurrency in Python

Section 3

Message passing: all the cool kids are doing it

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 10 / 27

Page 11: Message-passing concurrency in Python

UNIX

tiny1 $ cat mydata.csv | sort | uniq | wc -l

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 11 / 27

Page 12: Message-passing concurrency in Python

Go

tiny1 func main() {

tiny2 mychannel := make(chan string)

tiny3 go func() {

tiny4 mychannel <- "Hello world!"

tiny5 }()

tiny6 fmt.Println(<-mychannel)

tiny7 }

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 12 / 27

Page 13: Message-passing concurrency in Python

Rust

tiny1 fn main() {

tiny2 let (chan , port) = channel ();

tiny3

tiny4 spawn(proc() {

tiny5 chan.send("Hello world!");

tiny6 });

tiny7

tiny8 println !("{:s}", port.recv ());

tiny9 }

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 13 / 27

Page 14: Message-passing concurrency in Python

Scala

tiny1 object Example {

tiny2 class Example extends Actor {

tiny3 def act = {

tiny4 receive {

tiny5 case msg => println(msg)

tiny6 }

tiny7 }

tiny8 }

tiny9 def main(args:Array[String ]) {

tiny10 val a = new Example

tiny11 a.start

tiny12 a ! "Hello world!"

tiny13 }

tiny14 }Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 14 / 27

Page 15: Message-passing concurrency in Python

python-csp2

tiny1 @process

tiny2 def client(chan):

tiny3 print chan.read()

tiny4 @process

tiny5 def server(chan):

tiny6 chan.write(’Hello world!’)

tiny7

tiny8 if __name__ == ’__main__ ’:

tiny9 ch = Channel ()

tiny10 Par(client(ch), server(ch)). start()

2http://github.com/snim2/python-cspSarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 15 / 27

Page 16: Message-passing concurrency in Python

naulang3: building on the RPython toolchain

tiny1 let f = fn(mychannel) {

tiny2 mychannel <- "Hello world!"

tiny3 }

tiny4 let mychannel = chan()

tiny5 async f(mychannel)

tiny6 print <: mychannel

3Samuel Giles (2014) Is it possible to build a performant, dynamic language thatsupports concurrent models of computation? University of Wolverhampton BSc thesis

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 16 / 27

Page 17: Message-passing concurrency in Python

naulang4: building on the RPython toolchain

Table : Results from a tokenring benchmark @SamuelGiles

Min (µs) Max (µs) Mean (µs) s.d.

Go 1.1.2 99167 132488 100656 1590

Occam-π 68847 73355 69723 603

naulang 443316 486716 447894 6507naulang JIT 317510 589618 322304 10613naulang (async) 351677 490241 354325 6401naulang JIT (async) 42993 49496 44119 295

http://github.com/samgiles/naulang

4Samuel Giles (2014) Is it possible to build a performant, dynamic language thatsupports concurrent models of computation? University of Wolverhampton BSc thesis

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 17 / 27

Page 18: Message-passing concurrency in Python

Aside: Did those benchmarks matter?

That was a comparison between naulang and two compiled languages.The benchmark gives us some reason to suppose that a dynamic language(built on the RPython chain) can perform reasonably well compared tostatic languages.

Does this matter? We don’t want message passing to become abottleneck in optimising code. . .

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 18 / 27

Page 19: Message-passing concurrency in Python

Section 4

Design choices in message passing languages andlibraries

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 19 / 27

Page 20: Message-passing concurrency in Python

Synchronous vs. asynchronous channels

Synchronous channels block on read / write - examples include OCCAM,JCSP, python-csp, UNIX pipes. Asynchronous channels are non-blocking- examples include Scala, Rust. Some languages give you both - examplesinclude Go.Synchronous channels are easier to reason about (formally) and manypeople believe are easier to use. Most languagues include some extrafeatures to support synchronicity, e.g. if you are waiting to read from anumber of channels you can select or ALTernate one that is ready soonest.Aside: with some checks and balances to avoid starvation!

Asynchronous channels are sometimes thought to be faster. Like mostclaims about speed, this isn’t always true.

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 20 / 27

Page 21: Message-passing concurrency in Python

Uni or bidirectional channels? Point to point?

The well engineered Java CSP (JCSP) JCSP5 project separates outdifferent sorts of channels types, e.g.:

AnyToAnyChannel

AnyToOneChannel

OneToAnyChannel

OneToOneChannel

This isn’t very Pythonic, but it can catch errors in setting up a network ofchannels in a message passing system.

5http://www.cs.kent.ac.uk/projects/ofa/jcsp/Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 21 / 27

Page 22: Message-passing concurrency in Python

Mobile or immobile channels

A mobile channels can be sent over another channel at runtime. Thismeans that the network of channels can change its topology dynamicallyat runtime. Why might we want to do this?

our processes are actually running over a network of machines or on amanycore processor. We want to migrate some processes from onenode to another to perform load balancing or due to some failurecondition within a network.

some processes have finished their work and need to be poisoned(destroyed) and any channels associated with them need to be either(destroyed) or re-routed elsewhere. Aside: what happens if we wantto poison processes and channels that form a cyclic graph? What ifone process dies before it can poison others connected to it?

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 22 / 27

Page 23: Message-passing concurrency in Python

Reifying sequential processesIn CSP and actor model languages the paradigm is of a number ofsequential processes which run in parallel and communicate via channels(think: UNIX processes pipes). It is up to the language designer how toreify those sequential processes. Common choices are:

1 CSP process is 1 coroutine very fast message passing (OCCAM-πreports a small number of ns), cannot take advantage ofmulticore.

1 CSP process is 1 OS thread slower message passing; can take advantageof multicore

1 CSP process is 1 OS process very slow message passing; can migrateCSP processes across a network to other machines

1 CSP processes is 1 coroutine many coroutines are multiplexed onto anOS thread

1 CSP processes is 1 OS thread many OS threads are multiplexed onto anOS process

etc.Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 23 / 27

Page 24: Message-passing concurrency in Python

Section 5

Message passing in Python too!

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 24 / 27

Page 25: Message-passing concurrency in Python

So many EP14 talks!

Does Python have message passing? Sort of. . .Several EP14 talks involve coroutines (lightweight threads) and / orchannels, and I’m not even counting really cool innovations like PyPy-stm:

Daniel Pope Wedneday 11:00 (on gevent)

Christian Tismer, Anselm Kruis Wednesday 11:45 (on Stackless)

Richard Wall Friday 11:00 (on Twisted)

Benoit Chesneau Friday 12:00 (on Offset)

Dougal Matthew Friday 12:00 (on asyncio)

Message-passing has already entered the Python ecosystem and there areconstructs such as pipes and queues in the multiprocessing library.Could we do better by adopting some of the ideas above from otherlanguages?

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 25 / 27

Page 26: Message-passing concurrency in Python

The (near) future

Python for the Parallella will be coming out this Summer, thanks tofunding from the Royal Academy of Engineering. If you are interested inthis, please keep an eye on the futurecore6 GitHub organization.

python-csp is (slowly) moving back into regular development. Will it everbe fast?

naulang development continues athttp://github.com/samgiles/naulang I will be at the PyPy spring onSaturday working on this.

Pull requests and constructive criticism always welcome!

6http://github.com/futurecore/Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 26 / 27

Page 27: Message-passing concurrency in Python

Thank you.

Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 27 / 27


Recommended