SlideShare uma empresa Scribd logo
1 de 27
Baixar para ler offline
Message-passing concurrency in Python
Sarah Mount – @snim2
EuroPython 2014
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 1 / 27
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
Section 1
Why multicore?
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 3 / 27
Why multicore? (1/2)
Feature size (mm) vs time (year). CPU sizes are shrinking! Get the raw
data here: http://cpudb.stanford.edu/
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 4 / 27
Why multicore? (2/2)
New platforms, such as this board from Adapteva Inc. which reportedly
runs at 50GFPLOPS/Watt. Photo c Adapteva Inc.
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 5 / 27
Section 2
Message passing: an idea whose time has come (back)
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 6 / 27
Message passing concurrency
Tony Hoare
CSPa (Communicating Sequential Processes) was invented by Tony Hoare
in the late 70s / early 80s as a response to the difficulty of reasoning
about concurrent and parallel code. It was implemented as the OCCAM
programming language for the INMOS Transputer.
a
other process algebras are available
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 7 / 27
Message passing concurrency
CSP1 take the view that computation consists of communicating processes
which are individually made up of sequential instructions. The
communicating processes all run ”at the same time” and share no data.
Because they do not share data they have to cooperate by synchronising
on events. A common form of synchronisation (but not the only one) is to
pass data between processes via channels.
THINK UNIX processes communicating via pipes.
BEWARE CSP is an abstract ideas which use overloaded jargon, a
process or an event need not be reified as an OS process or GUI event. . .
1
other process algebras are available
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 8 / 27
Benefits of message passing
Threads and locking are tough! Deadlocks, starvation, race hazards are
tough. Locks do not compose easily, errors in locking cannot easily be
recovered from, finding the right level of granularity is hard.
CSP does not exhibit race hazards, there are no locks, the hairy details are
all hidden away in either a language runtime or a library. WIN!
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 9 / 27
Section 3
Message passing: all the cool kids are doing it
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 10 / 27
UNIX
1 $ cat mydata.csv | sort | uniq | wc -l
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 11 / 27
Go
1 func main () {
2 mychannel := make(chan string)
3 go func () {
4 mychannel <- "Hello world!"
5 }()
6 fmt.Println(<-mychannel)
7 }
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 12 / 27
Rust
1 fn main () {
2 let (chan , port) = channel ();
3
4 spawn(proc () {
5 chan.send("Hello world!");
6 });
7
8 println !("{:s}", port.recv ());
9 }
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 13 / 27
Scala
1 object Example {
2 class Example extends Actor {
3 def act = {
4 receive {
5 case msg => println(msg)
6 }
7 }
8 }
9 def main(args:Array[String ]) {
0 val a = new Example
1 a.start
2 a ! "Hello world!"
3 }
4 }
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 14 / 27
python-csp2
1 @process
2 def client(chan ):
3 print chan.read ()
4 @process
5 def server(chan ):
6 chan.write(’Hello world!’)
7
8 if __name__ == ’__main__ ’:
9 ch = Channel ()
0 Par(client(ch), server(ch)). start ()
2
http://github.com/snim2/python-csp
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 15 / 27
naulang3
: building on the RPython toolchain
1 let f = fn(mychannel) {
2 mychannel <- "Hello world!"
3 }
4 let mychannel = chan ()
5 async f(mychannel)
6 print <: mychannel
3
Samuel Giles (2014) Is it possible to build a performant, dynamic language that
supports concurrent models of computation? University of Wolverhampton BSc thesis
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 16 / 27
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 6507
naulang JIT 317510 589618 322304 10613
naulang (async) 351677 490241 354325 6401
naulang JIT (async) 42993 49496 44119 295
http://github.com/samgiles/naulang
4
Samuel Giles (2014) Is it possible to build a performant, dynamic language that
supports concurrent models of computation? University of Wolverhampton BSc thesis
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 17 / 27
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 to
static languages.
Does this matter? We don’t want message passing to become a
bottleneck in optimising code. . .
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 18 / 27
Section 4
Design choices in message passing languages and
libraries
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 19 / 27
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 - examples
include Go.
Synchronous channels are easier to reason about (formally) and many
people believe are easier to use. Most languagues include some extra
features to support synchronicity, e.g. if you are waiting to read from a
number 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 most
claims about speed, this isn’t always true.
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 20 / 27
Uni or bidirectional channels? Point to point?
The well engineered Java CSP (JCSP) JCSP5 project separates out
different 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 of
channels in a message passing system.
5
http://www.cs.kent.ac.uk/projects/ofa/jcsp/
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 21 / 27
Mobile or immobile channels
A mobile channels can be sent over another channel at runtime. This
means that the network of channels can change its topology dynamically
at runtime. Why might we want to do this?
our processes are actually running over a network of machines or on a
manycore processor. We want to migrate some processes from one
node to another to perform load balancing or due to some failure
condition 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 want
to poison processes and channels that form a cyclic graph? What if
one process dies before it can poison others connected to it?
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 22 / 27
Reifying sequential processes
In CSP and actor model languages the paradigm is of a number of
sequential processes which run in parallel and communicate via channels
(think: UNIX processes pipes). It is up to the language designer how to
reify 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 of
multicore.
1 CSP process is 1 OS thread slower message passing; can take advantage
of multicore
1 CSP process is 1 OS process very slow message passing; can migrate
CSP processes across a network to other machines
1 CSP processes is 1 coroutine many coroutines are multiplexed onto an
OS thread
1 CSP processes is 1 OS thread many OS threads are multiplexed onto an
OS process
etc.
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 23 / 27
Section 5
Message passing in Python too!
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 24 / 27
So many EP14 talks!
Does Python have message passing? Sort of. . .
Several EP14 talks involve coroutines (lightweight threads) and / or
channels, 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 are
constructs such as pipes and queues in the multiprocessing library.
Could we do better by adopting some of the ideas above from other
languages?
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 25 / 27
The (near) future
Python for the Parallella will be coming out this Summer, thanks to
funding from the Royal Academy of Engineering. If you are interested in
this, please keep an eye on the futurecore6 GitHub organization.
python-csp is (slowly) moving back into regular development. Will it ever
be fast?
naulang development continues at
http://github.com/samgiles/naulang I will be at the PyPy spring on
Saturday working on this.
Pull requests and constructive criticism always welcome!
6
http://github.com/futurecore/
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 26 / 27
Thank you.
Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 27 / 27

Mais conteúdo relacionado

Mais procurados

Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;
Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;
Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;Tzung-Bi Shih
 
DIY Deep Learning with Caffe Workshop
DIY Deep Learning with Caffe WorkshopDIY Deep Learning with Caffe Workshop
DIY Deep Learning with Caffe Workshopodsc
 
Attention mechanisms with tensorflow
Attention mechanisms with tensorflowAttention mechanisms with tensorflow
Attention mechanisms with tensorflowKeon Kim
 
A Language Support for Exhaustive Fault-Injection in Message-Passing System M...
A Language Support for Exhaustive Fault-Injection in Message-Passing System M...A Language Support for Exhaustive Fault-Injection in Message-Passing System M...
A Language Support for Exhaustive Fault-Injection in Message-Passing System M...Takuo Watanabe
 
Realtime processing with storm presentation
Realtime processing with storm presentationRealtime processing with storm presentation
Realtime processing with storm presentationGabriel Eisbruch
 
Python Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inPython Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inLearnbayin
 
Towards an Integration of the Actor Model in an FRP Language for Small-Scale ...
Towards an Integration of the Actor Model in an FRP Language for Small-Scale ...Towards an Integration of the Actor Model in an FRP Language for Small-Scale ...
Towards an Integration of the Actor Model in an FRP Language for Small-Scale ...Takuo Watanabe
 
Introduction to Deep Learning with Python
Introduction to Deep Learning with PythonIntroduction to Deep Learning with Python
Introduction to Deep Learning with Pythonindico data
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsGraham Dumpleton
 
Efficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverEfficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverShuo Chen
 
Muduo network library
Muduo network libraryMuduo network library
Muduo network libraryShuo Chen
 
FireWorks workflow software
FireWorks workflow softwareFireWorks workflow software
FireWorks workflow softwareAnubhav Jain
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Shuo Chen
 
Task Parallel Library Data Flows
Task Parallel Library Data FlowsTask Parallel Library Data Flows
Task Parallel Library Data FlowsSANKARSAN BOSE
 
Introduction to Chainer 11 may,2018
Introduction to Chainer 11 may,2018Introduction to Chainer 11 may,2018
Introduction to Chainer 11 may,2018Preferred Networks
 

Mais procurados (20)

Storm
StormStorm
Storm
 
STORM
STORMSTORM
STORM
 
Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;
Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;
Global Interpreter Lock: Episode III - cat &lt; /dev/zero > GIL;
 
DIY Deep Learning with Caffe Workshop
DIY Deep Learning with Caffe WorkshopDIY Deep Learning with Caffe Workshop
DIY Deep Learning with Caffe Workshop
 
Attention mechanisms with tensorflow
Attention mechanisms with tensorflowAttention mechanisms with tensorflow
Attention mechanisms with tensorflow
 
A Language Support for Exhaustive Fault-Injection in Message-Passing System M...
A Language Support for Exhaustive Fault-Injection in Message-Passing System M...A Language Support for Exhaustive Fault-Injection in Message-Passing System M...
A Language Support for Exhaustive Fault-Injection in Message-Passing System M...
 
Realtime processing with storm presentation
Realtime processing with storm presentationRealtime processing with storm presentation
Realtime processing with storm presentation
 
Python Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inPython Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.in
 
Towards an Integration of the Actor Model in an FRP Language for Small-Scale ...
Towards an Integration of the Actor Model in an FRP Language for Small-Scale ...Towards an Integration of the Actor Model in an FRP Language for Small-Scale ...
Towards an Integration of the Actor Model in an FRP Language for Small-Scale ...
 
Open Source Debugging v1.3.2
Open Source Debugging v1.3.2Open Source Debugging v1.3.2
Open Source Debugging v1.3.2
 
Introduction to Deep Learning with Python
Introduction to Deep Learning with PythonIntroduction to Deep Learning with Python
Introduction to Deep Learning with Python
 
Golang design4concurrency
Golang design4concurrencyGolang design4concurrency
Golang design4concurrency
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
 
Efficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverEfficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ server
 
Muduo network library
Muduo network libraryMuduo network library
Muduo network library
 
FireWorks workflow software
FireWorks workflow softwareFireWorks workflow software
FireWorks workflow software
 
Zurg part 1
Zurg part 1Zurg part 1
Zurg part 1
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
 
Task Parallel Library Data Flows
Task Parallel Library Data FlowsTask Parallel Library Data Flows
Task Parallel Library Data Flows
 
Introduction to Chainer 11 may,2018
Introduction to Chainer 11 may,2018Introduction to Chainer 11 may,2018
Introduction to Chainer 11 may,2018
 

Destaque

Mining python-software-pyconuk13
Mining python-software-pyconuk13Mining python-software-pyconuk13
Mining python-software-pyconuk13Sarah Mount
 
Scala - just good for Java shops?
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?Sarah Mount
 
Europython lightening talk_on_open_ihm
Europython lightening talk_on_open_ihmEuropython lightening talk_on_open_ihm
Europython lightening talk_on_open_ihmSarah Mount
 
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)An Introduction to Object-Oriented Programming (DrupalCamp North 2015)
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)Bart Feenstra
 
The (unknown) collections module
The (unknown) collections moduleThe (unknown) collections module
The (unknown) collections modulePablo Enfedaque
 
EuroPython 2015 - Decorators demystified
EuroPython 2015 - Decorators demystifiedEuroPython 2015 - Decorators demystified
EuroPython 2015 - Decorators demystifiedPablo Enfedaque
 
Oop concepts classes_objects
Oop concepts classes_objectsOop concepts classes_objects
Oop concepts classes_objectsWilliam Olivier
 
Meta-Classes in Python
Meta-Classes in PythonMeta-Classes in Python
Meta-Classes in PythonGuy Wiener
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Samuel Fortier-Galarneau
 
Python decorators
Python decoratorsPython decorators
Python decoratorsAlex Su
 
Erlang Message Passing Concurrency, For The Win
Erlang  Message  Passing  Concurrency,  For  The  WinErlang  Message  Passing  Concurrency,  For  The  Win
Erlang Message Passing Concurrency, For The Winl xf
 
Building Distributed Systems in Scala
Building Distributed Systems in ScalaBuilding Distributed Systems in Scala
Building Distributed Systems in ScalaAlex Payne
 
Prepping the Analytics organization for Artificial Intelligence evolution
Prepping the Analytics organization for Artificial Intelligence evolutionPrepping the Analytics organization for Artificial Intelligence evolution
Prepping the Analytics organization for Artificial Intelligence evolutionRamkumar Ravichandran
 
QCon Rio - Machine Learning for Everyone
QCon Rio - Machine Learning for EveryoneQCon Rio - Machine Learning for Everyone
QCon Rio - Machine Learning for EveryoneDhiana Deva
 
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
Python for Image Understanding: Deep Learning with Convolutional Neural NetsPython for Image Understanding: Deep Learning with Convolutional Neural Nets
Python for Image Understanding: Deep Learning with Convolutional Neural NetsRoelof Pieters
 

Destaque (20)

Mining python-software-pyconuk13
Mining python-software-pyconuk13Mining python-software-pyconuk13
Mining python-software-pyconuk13
 
Scala - just good for Java shops?
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
Europython lightening talk_on_open_ihm
Europython lightening talk_on_open_ihmEuropython lightening talk_on_open_ihm
Europython lightening talk_on_open_ihm
 
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)An Introduction to Object-Oriented Programming (DrupalCamp North 2015)
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)
 
The (unknown) collections module
The (unknown) collections moduleThe (unknown) collections module
The (unknown) collections module
 
EuroPython 2015 - Decorators demystified
EuroPython 2015 - Decorators demystifiedEuroPython 2015 - Decorators demystified
EuroPython 2015 - Decorators demystified
 
Oop concepts classes_objects
Oop concepts classes_objectsOop concepts classes_objects
Oop concepts classes_objects
 
Meta-Classes in Python
Meta-Classes in PythonMeta-Classes in Python
Meta-Classes in Python
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
Erlang Message Passing Concurrency, For The Win
Erlang  Message  Passing  Concurrency,  For  The  WinErlang  Message  Passing  Concurrency,  For  The  Win
Erlang Message Passing Concurrency, For The Win
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
 
Building Distributed Systems in Scala
Building Distributed Systems in ScalaBuilding Distributed Systems in Scala
Building Distributed Systems in Scala
 
Python in Action (Part 1)
Python in Action (Part 1)Python in Action (Part 1)
Python in Action (Part 1)
 
Prepping the Analytics organization for Artificial Intelligence evolution
Prepping the Analytics organization for Artificial Intelligence evolutionPrepping the Analytics organization for Artificial Intelligence evolution
Prepping the Analytics organization for Artificial Intelligence evolution
 
Mastering Python 3 I/O (Version 2)
Mastering Python 3 I/O (Version 2)Mastering Python 3 I/O (Version 2)
Mastering Python 3 I/O (Version 2)
 
An Introduction to Python Concurrency
An Introduction to Python ConcurrencyAn Introduction to Python Concurrency
An Introduction to Python Concurrency
 
QCon Rio - Machine Learning for Everyone
QCon Rio - Machine Learning for EveryoneQCon Rio - Machine Learning for Everyone
QCon Rio - Machine Learning for Everyone
 
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
Python for Image Understanding: Deep Learning with Convolutional Neural NetsPython for Image Understanding: Deep Learning with Convolutional Neural Nets
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
 

Semelhante a Message-passing concurrency in Python

Containerizing HPC and AI applications using E4S and Performance Monitor tool
Containerizing HPC and AI applications using E4S and Performance Monitor toolContainerizing HPC and AI applications using E4S and Performance Monitor tool
Containerizing HPC and AI applications using E4S and Performance Monitor toolGanesan Narayanasamy
 
Simulating the behavior of satellite Internet links to small islands
Simulating the behavior of satellite Internet links to small islandsSimulating the behavior of satellite Internet links to small islands
Simulating the behavior of satellite Internet links to small islandsAPNIC
 
CSP as a Domain-Specific Language Embedded in Python and Jython
CSP as a Domain-Specific Language Embedded in Python and JythonCSP as a Domain-Specific Language Embedded in Python and Jython
CSP as a Domain-Specific Language Embedded in Python and JythonM H
 
A Distributed Simulation of P-Systems
A Distributed Simulation of P-SystemsA Distributed Simulation of P-Systems
A Distributed Simulation of P-SystemsApostolos Syropoulos
 
OSS EU: Deep Dive into Building Streaming Applications with Apache Pulsar
OSS EU:  Deep Dive into Building Streaming Applications with Apache PulsarOSS EU:  Deep Dive into Building Streaming Applications with Apache Pulsar
OSS EU: Deep Dive into Building Streaming Applications with Apache PulsarTimothy Spann
 
python-csp: bringing OCCAM to Python
python-csp: bringing OCCAM to Pythonpython-csp: bringing OCCAM to Python
python-csp: bringing OCCAM to PythonSarah Mount
 
Splunk Conf 2014 - Getting the message
Splunk Conf 2014 - Getting the messageSplunk Conf 2014 - Getting the message
Splunk Conf 2014 - Getting the messageDamien Dallimore
 
Performance Evaluation using TAU Performance System and E4S
Performance Evaluation using TAU Performance System and E4SPerformance Evaluation using TAU Performance System and E4S
Performance Evaluation using TAU Performance System and E4SGanesan Narayanasamy
 
Simulation of Scale-Free Networks
Simulation of Scale-Free NetworksSimulation of Scale-Free Networks
Simulation of Scale-Free NetworksGabriele D'Angelo
 
The Offensive Python: Practical Python for Penetration Testing
The Offensive Python: Practical Python for Penetration TestingThe Offensive Python: Practical Python for Penetration Testing
The Offensive Python: Practical Python for Penetration TestingSatria Ady Pradana
 
The Offensive Python - Practical Python for Penetration Testing
The Offensive Python - Practical Python for Penetration TestingThe Offensive Python - Practical Python for Penetration Testing
The Offensive Python - Practical Python for Penetration TestingSatria Ady Pradana
 
Python 101 for the .NET Developer
Python 101 for the .NET DeveloperPython 101 for the .NET Developer
Python 101 for the .NET DeveloperSarah Dutkiewicz
 
RestMS Introduction
RestMS IntroductionRestMS Introduction
RestMS Introductionpieterh
 
2023comp90024_workshop.pdf
2023comp90024_workshop.pdf2023comp90024_workshop.pdf
2023comp90024_workshop.pdfLevLafayette1
 

Semelhante a Message-passing concurrency in Python (20)

Containerizing HPC and AI applications using E4S and Performance Monitor tool
Containerizing HPC and AI applications using E4S and Performance Monitor toolContainerizing HPC and AI applications using E4S and Performance Monitor tool
Containerizing HPC and AI applications using E4S and Performance Monitor tool
 
Simulating the behavior of satellite Internet links to small islands
Simulating the behavior of satellite Internet links to small islandsSimulating the behavior of satellite Internet links to small islands
Simulating the behavior of satellite Internet links to small islands
 
CSP as a Domain-Specific Language Embedded in Python and Jython
CSP as a Domain-Specific Language Embedded in Python and JythonCSP as a Domain-Specific Language Embedded in Python and Jython
CSP as a Domain-Specific Language Embedded in Python and Jython
 
A Distributed Simulation of P-Systems
A Distributed Simulation of P-SystemsA Distributed Simulation of P-Systems
A Distributed Simulation of P-Systems
 
OSS EU: Deep Dive into Building Streaming Applications with Apache Pulsar
OSS EU:  Deep Dive into Building Streaming Applications with Apache PulsarOSS EU:  Deep Dive into Building Streaming Applications with Apache Pulsar
OSS EU: Deep Dive into Building Streaming Applications with Apache Pulsar
 
Mpi Test Suite Multi Threaded
Mpi Test Suite Multi ThreadedMpi Test Suite Multi Threaded
Mpi Test Suite Multi Threaded
 
python-csp: bringing OCCAM to Python
python-csp: bringing OCCAM to Pythonpython-csp: bringing OCCAM to Python
python-csp: bringing OCCAM to Python
 
OpenFOAM Training v5-1-en
OpenFOAM Training v5-1-enOpenFOAM Training v5-1-en
OpenFOAM Training v5-1-en
 
Splunk Conf 2014 - Getting the message
Splunk Conf 2014 - Getting the messageSplunk Conf 2014 - Getting the message
Splunk Conf 2014 - Getting the message
 
Performance Evaluation using TAU Performance System and E4S
Performance Evaluation using TAU Performance System and E4SPerformance Evaluation using TAU Performance System and E4S
Performance Evaluation using TAU Performance System and E4S
 
More mpi4py
More mpi4pyMore mpi4py
More mpi4py
 
Simulation of Scale-Free Networks
Simulation of Scale-Free NetworksSimulation of Scale-Free Networks
Simulation of Scale-Free Networks
 
The Offensive Python: Practical Python for Penetration Testing
The Offensive Python: Practical Python for Penetration TestingThe Offensive Python: Practical Python for Penetration Testing
The Offensive Python: Practical Python for Penetration Testing
 
The Offensive Python - Practical Python for Penetration Testing
The Offensive Python - Practical Python for Penetration TestingThe Offensive Python - Practical Python for Penetration Testing
The Offensive Python - Practical Python for Penetration Testing
 
Python 101 for the .NET Developer
Python 101 for the .NET DeveloperPython 101 for the .NET Developer
Python 101 for the .NET Developer
 
RestMS Introduction
RestMS IntroductionRestMS Introduction
RestMS Introduction
 
2023comp90024_workshop.pdf
2023comp90024_workshop.pdf2023comp90024_workshop.pdf
2023comp90024_workshop.pdf
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Pythonfinalppt 170822121204
Pythonfinalppt 170822121204Pythonfinalppt 170822121204
Pythonfinalppt 170822121204
 
Lec9
Lec9Lec9
Lec9
 

Message-passing concurrency in Python

  • 1. Message-passing concurrency in Python Sarah Mount – @snim2 EuroPython 2014 Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 1 / 27
  • 2. 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
  • 3. Section 1 Why multicore? Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 3 / 27
  • 4. Why multicore? (1/2) Feature size (mm) vs time (year). CPU sizes are shrinking! Get the raw data here: http://cpudb.stanford.edu/ Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 4 / 27
  • 5. Why multicore? (2/2) New platforms, such as this board from Adapteva Inc. which reportedly runs at 50GFPLOPS/Watt. Photo c Adapteva Inc. Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 5 / 27
  • 6. Section 2 Message passing: an idea whose time has come (back) Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 6 / 27
  • 7. Message passing concurrency Tony Hoare CSPa (Communicating Sequential Processes) was invented by Tony Hoare in the late 70s / early 80s as a response to the difficulty of reasoning about concurrent and parallel code. It was implemented as the OCCAM programming language for the INMOS Transputer. a other process algebras are available Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 7 / 27
  • 8. Message passing concurrency CSP1 take the view that computation consists of communicating processes which are individually made up of sequential instructions. The communicating processes all run ”at the same time” and share no data. Because they do not share data they have to cooperate by synchronising on events. A common form of synchronisation (but not the only one) is to pass data between processes via channels. THINK UNIX processes communicating via pipes. BEWARE CSP is an abstract ideas which use overloaded jargon, a process or an event need not be reified as an OS process or GUI event. . . 1 other process algebras are available Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 8 / 27
  • 9. Benefits of message passing Threads and locking are tough! Deadlocks, starvation, race hazards are tough. Locks do not compose easily, errors in locking cannot easily be recovered from, finding the right level of granularity is hard. CSP does not exhibit race hazards, there are no locks, the hairy details are all hidden away in either a language runtime or a library. WIN! Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 9 / 27
  • 10. Section 3 Message passing: all the cool kids are doing it Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 10 / 27
  • 11. UNIX 1 $ cat mydata.csv | sort | uniq | wc -l Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 11 / 27
  • 12. Go 1 func main () { 2 mychannel := make(chan string) 3 go func () { 4 mychannel <- "Hello world!" 5 }() 6 fmt.Println(<-mychannel) 7 } Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 12 / 27
  • 13. Rust 1 fn main () { 2 let (chan , port) = channel (); 3 4 spawn(proc () { 5 chan.send("Hello world!"); 6 }); 7 8 println !("{:s}", port.recv ()); 9 } Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 13 / 27
  • 14. Scala 1 object Example { 2 class Example extends Actor { 3 def act = { 4 receive { 5 case msg => println(msg) 6 } 7 } 8 } 9 def main(args:Array[String ]) { 0 val a = new Example 1 a.start 2 a ! "Hello world!" 3 } 4 } Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 14 / 27
  • 15. python-csp2 1 @process 2 def client(chan ): 3 print chan.read () 4 @process 5 def server(chan ): 6 chan.write(’Hello world!’) 7 8 if __name__ == ’__main__ ’: 9 ch = Channel () 0 Par(client(ch), server(ch)). start () 2 http://github.com/snim2/python-csp Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 15 / 27
  • 16. naulang3 : building on the RPython toolchain 1 let f = fn(mychannel) { 2 mychannel <- "Hello world!" 3 } 4 let mychannel = chan () 5 async f(mychannel) 6 print <: mychannel 3 Samuel Giles (2014) Is it possible to build a performant, dynamic language that supports concurrent models of computation? University of Wolverhampton BSc thesis Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 16 / 27
  • 17. 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 6507 naulang JIT 317510 589618 322304 10613 naulang (async) 351677 490241 354325 6401 naulang JIT (async) 42993 49496 44119 295 http://github.com/samgiles/naulang 4 Samuel Giles (2014) Is it possible to build a performant, dynamic language that supports concurrent models of computation? University of Wolverhampton BSc thesis Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 17 / 27
  • 18. 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 to static languages. Does this matter? We don’t want message passing to become a bottleneck in optimising code. . . Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 18 / 27
  • 19. Section 4 Design choices in message passing languages and libraries Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 19 / 27
  • 20. 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 - examples include Go. Synchronous channels are easier to reason about (formally) and many people believe are easier to use. Most languagues include some extra features to support synchronicity, e.g. if you are waiting to read from a number 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 most claims about speed, this isn’t always true. Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 20 / 27
  • 21. Uni or bidirectional channels? Point to point? The well engineered Java CSP (JCSP) JCSP5 project separates out different 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 of channels in a message passing system. 5 http://www.cs.kent.ac.uk/projects/ofa/jcsp/ Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 21 / 27
  • 22. Mobile or immobile channels A mobile channels can be sent over another channel at runtime. This means that the network of channels can change its topology dynamically at runtime. Why might we want to do this? our processes are actually running over a network of machines or on a manycore processor. We want to migrate some processes from one node to another to perform load balancing or due to some failure condition 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 want to poison processes and channels that form a cyclic graph? What if one process dies before it can poison others connected to it? Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 22 / 27
  • 23. Reifying sequential processes In CSP and actor model languages the paradigm is of a number of sequential processes which run in parallel and communicate via channels (think: UNIX processes pipes). It is up to the language designer how to reify 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 of multicore. 1 CSP process is 1 OS thread slower message passing; can take advantage of multicore 1 CSP process is 1 OS process very slow message passing; can migrate CSP processes across a network to other machines 1 CSP processes is 1 coroutine many coroutines are multiplexed onto an OS thread 1 CSP processes is 1 OS thread many OS threads are multiplexed onto an OS process etc. Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 23 / 27
  • 24. Section 5 Message passing in Python too! Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 24 / 27
  • 25. So many EP14 talks! Does Python have message passing? Sort of. . . Several EP14 talks involve coroutines (lightweight threads) and / or channels, 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 are constructs such as pipes and queues in the multiprocessing library. Could we do better by adopting some of the ideas above from other languages? Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 25 / 27
  • 26. The (near) future Python for the Parallella will be coming out this Summer, thanks to funding from the Royal Academy of Engineering. If you are interested in this, please keep an eye on the futurecore6 GitHub organization. python-csp is (slowly) moving back into regular development. Will it ever be fast? naulang development continues at http://github.com/samgiles/naulang I will be at the PyPy spring on Saturday working on this. Pull requests and constructive criticism always welcome! 6 http://github.com/futurecore/ Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 26 / 27
  • 27. Thank you. Sarah Mount – @snim2 Message-passing concurrency in Python EuroPython 2014 27 / 27