python-csp: bringing OCCAM to Python

Sarah Mount
Sarah MountSenior Lecturer in Computer Science at University of Wolverhampton
Ancient history
    python-csp: features and idioms
            Using built-in processes
                  Future challenges




python-csp: bringing OCCAM to Python

                            Sarah Mount


                        Europython 2010




                       Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
            python-csp: features and idioms
                    Using built-in processes
                          Future challenges




1 Ancient history

2 python-csp: features and idioms
     Process creation
     Parallel, sequential and repeated processes
     Communication via channel read / writes
     More channels: ALTing and choice
     More channels: poisoning
     Producer / consumer or worker / farmer models

3 Using built-in processes

4 Future challenges



                               Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
                 python-csp: features and idioms
                         Using built-in processes
                               Future challenges




This talk. . .
is all about the python-csp project. There is a similar project
called PyCSP, these will merge over the next few months. Current
code base is here: http://code.google.com/p/python-csp
Please do contribute bug fixes / issues / code / docs / rants / . . .




                                    Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
            python-csp: features and idioms
                    Using built-in processes
                          Future challenges




INMOS B0042 Board c David May
The Transputer was the original “multicore” machine and was built
to run the OCCAM language – a realisation of CSP.
http://www.cs.bris.ac.uk/~dave/transputer.html
                               Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
             python-csp: features and idioms
                     Using built-in processes
                           Future challenges




Tony Hoare
Invented CSP, OCCAM, Quicksort and other baddass stuff. You
may remember him from such talks as his Europython 2009
Keynote.
                                Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
             python-csp: features and idioms
                     Using built-in processes
                           Future challenges




OCCAM



 OCCAM lives on in it’s current implementation as OCCAM-π
 http://www.cs.kent.ac.uk/projects/ofa/kroc/
 It can run on Lego bricks, Arduino boards and other things. Like
 Python it uses indentation to demark scope. Unlike Python
 OCCAM is MOSTLY IN UPPERCASE.




                                Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
                    python-csp: features and idioms
                            Using built-in processes
                                  Future challenges




CSP and message passing
This next bit will be revision for many of you!




                                       Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
             python-csp: features and idioms
                     Using built-in processes
                           Future challenges




Most of us think of programs like this




   http://xkcd.com/205/

                                Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
             python-csp: features and idioms
                     Using built-in processes
                           Future challenges




This is the old “standard” model!



      Multicore will soon take over the world (if it hasn’t already).
      Shared memory concurrency / parallelism is hard:
          Race hazards
          Deadlock
          Livelock
          Tracking which lock goes with which data and what order
          locks should be used.
          Operating Systems are old news!




                                Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
               python-csp: features and idioms
                       Using built-in processes
                             Future challenges




Message passing
   Definition
   In message passing concurrency (or parallelism) “everything” is a
   process and no data is shared between processes. Instead, data is
   “passed” between channels which connect processes together.
   Think: OS processes and UNIX pipes.

       The actor model (similar to Kamaelia) uses asynchronous
       channels. Processes write data to a channel and continue
       execution.
       CSP (and π-calculus) use synchronous channels, or
       rendezvous, where a process writing to a channel has to wait
       until the value has been read by another process before
       proceeding.
                                  Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
           python-csp: features and idioms
                   Using built-in processes
                         Future challenges




Process diagram




                              Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
               python-csp: features and idioms
                       Using built-in processes
                             Future challenges




Why synchronous channels?




   Leslie Lamport (1978). Time, clocks, and the ordering of events in
   a distributed system”. Communications of the ACM 21 (7)

                                  Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                               Ancient history    Parallel, sequential and repeated processes
               python-csp: features and idioms    Communication via channel read / writes
                       Using built-in processes   More channels: ALTing and choice
                             Future challenges    More channels: poisoning
                                                  Producer / consumer or worker / farmer models


Hello World!



      from c s p . c s p p r o c e s s i m p o r t ∗

      @process
      def h e l l o () :
          p r i n t ’ H e l l o CSP w o r l d ! ’

      hello () . start ()




                                  Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                               Ancient history    Parallel, sequential and repeated processes
               python-csp: features and idioms    Communication via channel read / writes
                       Using built-in processes   More channels: ALTing and choice
                             Future challenges    More channels: poisoning
                                                  Producer / consumer or worker / farmer models


Example of process creation: web server

      @process
      def s e r v e r ( host , port ) :
        sock = socket . socket ( . . . )
        # S e t up s o c k e t s
        w h i l e True :
                conn , a d d r = s o c k . a c c e p t ( )
                r e q u e s t = conn . r e c v ( 4 0 9 6 ) . s t r i p ( )
                i f r e q u e s t . s t a r t s w i t h ( ’GET ’ ) :
                       ok ( r e q u e s t , conn ) . s t a r t ( )
                else :
                        n o t f o u n d ( r e q u e s t , conn ) . s t a r t ( )


                                  Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                              Ancient history    Parallel, sequential and repeated processes
              python-csp: features and idioms    Communication via channel read / writes
                      Using built-in processes   More channels: ALTing and choice
                            Future challenges    More channels: poisoning
                                                 Producer / consumer or worker / farmer models


Example of process creation: web server


      @process
      d e f n o t f o u n d ( r e q u e s t , conn ) :
            page = ’<h1>F i l e n o t found </h1> ’
            conn . s e n d ( r e s p o n s e ( 4 0 4 ,
                                               ’ Not Found ’ ,
                                               page ) )
            conn . shutdown ( s o c k e t . SHUT RDWR)
            conn . c l o s e ( )
            return



                                 Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                             Ancient history    Parallel, sequential and repeated processes
             python-csp: features and idioms    Communication via channel read / writes
                     Using built-in processes   More channels: ALTing and choice
                           Future challenges    More channels: poisoning
                                                Producer / consumer or worker / farmer models


Combining processes: in parallel



           # With a Par o b j e c t :
           Par ( p1 , p2 , p3 , . . . pn ) . s t a r t ( )
           # With s y n t a c t i c s u g a r :
           p1 // ( p2 , p3 , . . . pn )
           # RHS must be a s e q u e n c e t y p e .




                                Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                               Ancient history    Parallel, sequential and repeated processes
               python-csp: features and idioms    Communication via channel read / writes
                       Using built-in processes   More channels: ALTing and choice
                             Future challenges    More channels: poisoning
                                                  Producer / consumer or worker / farmer models


Combining processes: repeating a process sequentially



       @process
       def h e l l o () :
           p r i n t ’ H e l l o CSP w o r l d ! ’
       if   name          == ’ m a i n ’ :
           hello () ∗ 3
           2 ∗ hello ()




                                  Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                              Ancient history    Parallel, sequential and repeated processes
              python-csp: features and idioms    Communication via channel read / writes
                      Using built-in processes   More channels: ALTing and choice
                            Future challenges    More channels: poisoning
                                                 Producer / consumer or worker / farmer models


Channels


      @process
      d e f c l i e n t ( chan ) :
            p r i n t chan . r e a d ( )
      @process
      d e f s e r v e r ( chan ) :
            chan . w r i t e ( ’ H e l l o CSP w o r l d ! ’ )

      if     name         == ’ m a i n ’ :
            ch = C h a n n e l ( )
            Par ( c l i e n t ( ch ) , s e r v e r ( ch ) ) . s t a r t ( )


                                 Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                             Ancient history    Parallel, sequential and repeated processes
             python-csp: features and idioms    Communication via channel read / writes
                     Using built-in processes   More channels: ALTing and choice
                           Future challenges    More channels: poisoning
                                                Producer / consumer or worker / farmer models


What you need to know about channels


      Channels are currently UNIX pipes
      This will likely change
      Channel rendezvous is “slow” compared with JCSP:
      200µ s vs 16µ s
      This will be fixed by having channels written in C
      Because channels are, at the OS level, open “files”, there can
      only be a limited number of them (around 300 on my
      machine)
      This makes me sad :-(


                                Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                               Ancient history    Parallel, sequential and repeated processes
               python-csp: features and idioms    Communication via channel read / writes
                       Using built-in processes   More channels: ALTing and choice
                             Future challenges    More channels: poisoning
                                                  Producer / consumer or worker / farmer models


Message passing an responsiveness

       This isn’t about speed.
       Sometimes, you have several channels and reading from them
       all round-robin may reduce responsiveness if one channel read
       blocks for a long time.
   This is BAD:

       @process
       def foo ( channels ) :
           f o r chan i n c h a n n e l s :
                 p r i n t chan . r e a d ( )


                                  Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                               Ancient history    Parallel, sequential and repeated processes
               python-csp: features and idioms    Communication via channel read / writes
                       Using built-in processes   More channels: ALTing and choice
                             Future challenges    More channels: poisoning
                                                  Producer / consumer or worker / farmer models


ALTing and choice

   Definition
   ALTing, or non-deterministic choice, selects a channel to read from
   from those channels which are ready to read from.

   You can do this with (only) two channels:

       @process
       d e f f o o ( c1 , c2 ) :
             p r i n t c1 | c2
             p r i n t c1 | c2



                                  Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                              Ancient history    Parallel, sequential and repeated processes
              python-csp: features and idioms    Communication via channel read / writes
                      Using built-in processes   More channels: ALTing and choice
                            Future challenges    More channels: poisoning
                                                 Producer / consumer or worker / farmer models


ALTing proper (many channels)


      @process
      d e f f o o ( c1 , c2 , c3 , c4 , c5 ) :
            a l t = A l t ( c1 , c2 , c3 , c4 , c5 )
            # Choose random a v a i l a b l e o f f e r
            print alt . se le ct ()
            # Avoid l a s t s e l e c t e d channel
            print alt . f a i r s e l e c t ()
            # Choose c h a n n e l w i t h l o w e s t i n d e x
            print alt . p r i s e l e c t ()



                                 Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                             Ancient history    Parallel, sequential and repeated processes
             python-csp: features and idioms    Communication via channel read / writes
                     Using built-in processes   More channels: ALTing and choice
                           Future challenges    More channels: poisoning
                                                Producer / consumer or worker / farmer models


Syntactic sugar for ALTing


      @process
      d e f f o o ( c1 , c2 , c3 , c4 , c5 ) :
            gen = A l t ( c1 , c2 , c3 , c4 , c5 )
            p r i n t gen . n e x t ( )
            p r i n t gen . n e x t ( )
            p r i n t gen . n e x t ( )
            p r i n t gen . n e x t ( )
            p r i n t gen . n e x t ( )




                                Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                              Ancient history    Parallel, sequential and repeated processes
              python-csp: features and idioms    Communication via channel read / writes
                      Using built-in processes   More channels: ALTing and choice
                            Future challenges    More channels: poisoning
                                                 Producer / consumer or worker / farmer models


ALTing: when you really, really must return right now



       @process
       d e f f o o ( c1 , c2 , c3 ) :
             # Skip () w i l l / always / complete
             # a read immediately
             gen = A l t ( c1 , c2 , c3 , S k i p ( ) )
             p r i n t gen . n e x t ( )
             ...




                                 Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                               Ancient history    Parallel, sequential and repeated processes
               python-csp: features and idioms    Communication via channel read / writes
                       Using built-in processes   More channels: ALTing and choice
                             Future challenges    More channels: poisoning
                                                  Producer / consumer or worker / farmer models


Channel poisoning
   Definition
   Idiomatically in this style of programming most processes contain
   infinite loops. Poisoning a channel asks all processes connected to
   that channel to shut down automatically. Each process which has
   a poisoned channel will automatically poison any other channels it
   is connected to. This way, a whole graph of connected processes
   can be terminated, avoiding race conditions.

       @process
       def foo ( channel ) :
           ...
           channel . poison ()

                                  Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                            Ancient history    Parallel, sequential and repeated processes
            python-csp: features and idioms    Communication via channel read / writes
                    Using built-in processes   More channels: ALTing and choice
                          Future challenges    More channels: poisoning
                                               Producer / consumer or worker / farmer models


A bigger example: Mandelbrot generator




                               Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                         Ancient history    Parallel, sequential and repeated processes
         python-csp: features and idioms    Communication via channel read / writes
                 Using built-in processes   More channels: ALTing and choice
                       Future challenges    More channels: poisoning
                                            Producer / consumer or worker / farmer models




@process
d e f main ( IMSIZE ) :
      channels = [ ]
      # One p r o d u c e r + c h a n n e l f o r e a c h
            image column .
      f o r x i n r a n g e ( IMSIZE [ 0 ] ) :
              c h a n n e l s . append ( C h a n n e l ( ) )
              p r o c e s s e s . append ( m a n d e l b r o t ( x ,
                    . . . channels [ x ]) )
      p r o c e s s e s . i n s e r t ( 0 , consume ( IMSIZE ,
            channels ) )
      mandel = Par ( ∗ p r o c e s s e s )
      mandel . s t a r t ( )


                            Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                      Ancient history    Parallel, sequential and repeated processes
      python-csp: features and idioms    Communication via channel read / writes
              Using built-in processes   More channels: ALTing and choice
                    Future challenges    More channels: poisoning
                                         Producer / consumer or worker / farmer models




@process
def mandelbrot ( xcoord , cout ) :
    # Do some maths h e r e
    cout . w r i t e ( xcoord , column data )




                         Sarah Mount     python-csp: bringing OCCAM to Python
Process creation
                        Ancient history    Parallel, sequential and repeated processes
        python-csp: features and idioms    Communication via channel read / writes
                Using built-in processes   More channels: ALTing and choice
                      Future challenges    More channels: poisoning
                                           Producer / consumer or worker / farmer models




@process
d e f consumer ( c i n s ) :
      # I n i t i a l i s e PyGame . . .
      gen = l e n ( c i n s ) ∗ A l t ( ∗ c i n s )
      f o r i in range ( len ( c i n s ) ) :
            x c o o r d , column = gen . n e x t ( )
            # B l i t t h a t column t o s c r e e n
      f o r e v e n t i n pygame . e v e n t . g e t ( ) :
            i f e v e n t . t y p e == pygame . QUIT :
                    for channel in cins :
                           channel . poison ()
                    pygame . q u i t ( )


                           Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
               python-csp: features and idioms
                       Using built-in processes
                             Future challenges




Oscilloscope




                                  Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
         python-csp: features and idioms
                 Using built-in processes
                       Future challenges




@process
def O s c i ll o s c o p e ( inchan ) :
    # I n i t i a l i s e PyGame
    ydata = [ ]
    w h i l e n o t QUIT :
            y d a t a . append ( i n c h a n . r e a d ( ) )
            y d a t a . pop ( 0 )
           # B l i t data to s c r e e n . . .
    # Deal with e v e n t s




                            Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
         python-csp: features and idioms
                 Using built-in processes
                       Future challenges




from c s p . b u i l t i n s i m p o r t S i n
def t r a c e s i n () :
    chans = Channel ( ) , Channel ( )
    Par ( G e n e r a t e F l o a t s ( c h a n s [ 0 ] ) ,
            Sin ( chans [ 0 ] , chans [ 1 ] ) ,
            O s c i l l o s c o p e ( chans [ 1 ] ) ) . s t a r t ()
    return




                            Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
           python-csp: features and idioms
                   Using built-in processes
                         Future challenges




Process diagram




                              Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
        python-csp: features and idioms
                Using built-in processes
                      Future challenges




def trace mux () :
    chans = [ Channel ( ) f o r i i n range (6) ]
    p a r = Par ( G e n e r a t e F l o a t s ( c h a n s [ 0 ] ) ,
                     Delta2 ( chans [ 0 ] , chans [ 1 ] ,
                                    chans [ 2 ] ) ,
                     Cos ( c h a n s [ 1 ] , c h a n s [ 3 ] ) ,
                     Sin ( chans [ 2 ] , chans [ 4 ] ) ,
                     Mux2 ( c h a n s [ 3 ] , c h a n s [ 4 ] ,
                               chans [ 5 ] ) ,
                     O s c i l l o s c o p e ( chans [ 5 ] ) )
    par . s t a r t ()



                           Sarah Mount     python-csp: bringing OCCAM to Python
Ancient history
         python-csp: features and idioms
                 Using built-in processes
                       Future challenges




Wiring




                            Sarah Mount     python-csp: bringing OCCAM to Python
1 of 35

Recommended

Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O... by
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...Igalia
324 views42 slides
Atlanta Spark User Meetup 09 22 2016 by
Atlanta Spark User Meetup 09 22 2016Atlanta Spark User Meetup 09 22 2016
Atlanta Spark User Meetup 09 22 2016Chris Fregly
576 views72 slides
Caffe - A deep learning framework (Ramin Fahimi) by
Caffe - A deep learning framework (Ramin Fahimi)Caffe - A deep learning framework (Ramin Fahimi)
Caffe - A deep learning framework (Ramin Fahimi)irpycon
2.4K views42 slides
OpenHFT: An Advanced Java Data Locality and IPC Transport Solution by
OpenHFT: An Advanced Java Data Locality and IPC Transport SolutionOpenHFT: An Advanced Java Data Locality and IPC Transport Solution
OpenHFT: An Advanced Java Data Locality and IPC Transport SolutionBen Cotton
857 views37 slides
Wireshark display filters by
Wireshark display filtersWireshark display filters
Wireshark display filtersMohamed Gamel
279 views2 slides
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016 by
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016MLconf
1.4K views42 slides

More Related Content

What's hot

Lp seminar by
Lp seminarLp seminar
Lp seminarguestdff961
405 views47 slides
USENIX Vault'19: Performance analysis in Linux storage stack with BPF by
USENIX Vault'19: Performance analysis in Linux storage stack with BPFUSENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPFTaeung Song
1.4K views240 slides
carrow - Go bindings to Apache Arrow via C++-API by
carrow - Go bindings to Apache Arrow via C++-APIcarrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIYoni Davidson
452 views41 slides
Kalman Graffi - IEEE HPCS 2013 - Comparative Evaluation of P2P Systems Using ... by
Kalman Graffi - IEEE HPCS 2013 - Comparative Evaluation of P2P Systems Using ...Kalman Graffi - IEEE HPCS 2013 - Comparative Evaluation of P2P Systems Using ...
Kalman Graffi - IEEE HPCS 2013 - Comparative Evaluation of P2P Systems Using ...Kalman Graffi
325 views34 slides
PyPy London Demo Evening 2013 by
PyPy London Demo Evening 2013PyPy London Demo Evening 2013
PyPy London Demo Evening 2013Carl Friedrich Bolz
77.8K views53 slides
Plane Spotting by
Plane SpottingPlane Spotting
Plane SpottingTed Coyle
53 views48 slides

What's hot(7)

USENIX Vault'19: Performance analysis in Linux storage stack with BPF by Taeung Song
USENIX Vault'19: Performance analysis in Linux storage stack with BPFUSENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPF
Taeung Song1.4K views
carrow - Go bindings to Apache Arrow via C++-API by Yoni Davidson
carrow - Go bindings to Apache Arrow via C++-APIcarrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-API
Yoni Davidson452 views
Kalman Graffi - IEEE HPCS 2013 - Comparative Evaluation of P2P Systems Using ... by Kalman Graffi
Kalman Graffi - IEEE HPCS 2013 - Comparative Evaluation of P2P Systems Using ...Kalman Graffi - IEEE HPCS 2013 - Comparative Evaluation of P2P Systems Using ...
Kalman Graffi - IEEE HPCS 2013 - Comparative Evaluation of P2P Systems Using ...
Kalman Graffi325 views
Plane Spotting by Ted Coyle
Plane SpottingPlane Spotting
Plane Spotting
Ted Coyle53 views
Jvm tuning in a rush! - Lviv JUG by Tomek Borek
Jvm tuning in a rush! - Lviv JUGJvm tuning in a rush! - Lviv JUG
Jvm tuning in a rush! - Lviv JUG
Tomek Borek1.8K views

Similar to python-csp: bringing OCCAM to Python

Message-passing concurrency in Python by
Message-passing concurrency in PythonMessage-passing concurrency in Python
Message-passing concurrency in PythonSarah Mount
9.4K views27 slides
All Day DevOps - FLiP Stack for Cloud Data Lakes by
All Day DevOps - FLiP Stack for Cloud Data LakesAll Day DevOps - FLiP Stack for Cloud Data Lakes
All Day DevOps - FLiP Stack for Cloud Data LakesTimothy Spann
491 views31 slides
Numba lightning by
Numba lightningNumba lightning
Numba lightningTravis Oliphant
1.4K views11 slides
Streaming 101: Hello World by
Streaming 101:  Hello WorldStreaming 101:  Hello World
Streaming 101: Hello WorldJosh Fischer
101 views24 slides
OSS EU: Deep Dive into Building Streaming Applications with Apache Pulsar by
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
822 views56 slides
Deep Dive into Building Streaming Applications with Apache Pulsar by
Deep Dive into Building Streaming Applications with Apache Pulsar Deep Dive into Building Streaming Applications with Apache Pulsar
Deep Dive into Building Streaming Applications with Apache Pulsar Timothy Spann
298 views61 slides

Similar to python-csp: bringing OCCAM to Python(20)

Message-passing concurrency in Python by Sarah Mount
Message-passing concurrency in PythonMessage-passing concurrency in Python
Message-passing concurrency in Python
Sarah Mount9.4K views
All Day DevOps - FLiP Stack for Cloud Data Lakes by Timothy Spann
All Day DevOps - FLiP Stack for Cloud Data LakesAll Day DevOps - FLiP Stack for Cloud Data Lakes
All Day DevOps - FLiP Stack for Cloud Data Lakes
Timothy Spann491 views
Streaming 101: Hello World by Josh Fischer
Streaming 101:  Hello WorldStreaming 101:  Hello World
Streaming 101: Hello World
Josh Fischer101 views
OSS EU: Deep Dive into Building Streaming Applications with Apache Pulsar by Timothy Spann
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
Timothy Spann822 views
Deep Dive into Building Streaming Applications with Apache Pulsar by Timothy Spann
Deep Dive into Building Streaming Applications with Apache Pulsar Deep Dive into Building Streaming Applications with Apache Pulsar
Deep Dive into Building Streaming Applications with Apache Pulsar
Timothy Spann298 views
Streaming Machine Learning with Python, Jupyter, TensorFlow, Apache Kafka and... by Kai Wähner
Streaming Machine Learning with Python, Jupyter, TensorFlow, Apache Kafka and...Streaming Machine Learning with Python, Jupyter, TensorFlow, Apache Kafka and...
Streaming Machine Learning with Python, Jupyter, TensorFlow, Apache Kafka and...
Kai Wähner1.9K views
Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018 by DevOpsDays Tel Aviv
Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018
Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018
What’s Slowing Down Your Kafka Pipeline? With Ruizhe Cheng and Pete Stevenson... by HostedbyConfluent
What’s Slowing Down Your Kafka Pipeline? With Ruizhe Cheng and Pete Stevenson...What’s Slowing Down Your Kafka Pipeline? With Ruizhe Cheng and Pete Stevenson...
What’s Slowing Down Your Kafka Pipeline? With Ruizhe Cheng and Pete Stevenson...
HostedbyConfluent350 views
Introduction to Apache Beam & No Shard Left Behind: APIs for Massive Parallel... by Dan Halperin
Introduction to Apache Beam & No Shard Left Behind: APIs for Massive Parallel...Introduction to Apache Beam & No Shard Left Behind: APIs for Massive Parallel...
Introduction to Apache Beam & No Shard Left Behind: APIs for Massive Parallel...
Dan Halperin1.4K views
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019 by Thomas Weise
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
Thomas Weise1.4K views
H2O World - What's New in H2O with Cliff Click by Sri Ambati
H2O World - What's New in H2O with Cliff ClickH2O World - What's New in H2O with Cliff Click
H2O World - What's New in H2O with Cliff Click
Sri Ambati1.6K views
Traitement temps réel chez Streamroot - Golang Paris Juin 2016 by Simon Caplette
Traitement temps réel chez Streamroot - Golang Paris Juin 2016Traitement temps réel chez Streamroot - Golang Paris Juin 2016
Traitement temps réel chez Streamroot - Golang Paris Juin 2016
Simon Caplette742 views
UCX-Python - A Flexible Communication Library for Python Applications by Matthew Rocklin
UCX-Python - A Flexible Communication Library for Python ApplicationsUCX-Python - A Flexible Communication Library for Python Applications
UCX-Python - A Flexible Communication Library for Python Applications
Matthew Rocklin1.1K views
3.2 Streaming and Messaging by 振东 刘
3.2 Streaming and Messaging3.2 Streaming and Messaging
3.2 Streaming and Messaging
振东 刘219 views
BigDataFest Building Modern Data Streaming Apps by ssuser73434e
BigDataFest  Building Modern Data Streaming AppsBigDataFest  Building Modern Data Streaming Apps
BigDataFest Building Modern Data Streaming Apps
ssuser73434e6 views
Euro python2011 High Performance Python by Ian Ozsvald
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance Python
Ian Ozsvald3K views
How to integrate python into a scala stack by Fliptop
How to integrate python into a scala stackHow to integrate python into a scala stack
How to integrate python into a scala stack
Fliptop7.2K views
ApacheCon2022_Deep Dive into Building Streaming Applications with Apache Pulsar by Timothy Spann
ApacheCon2022_Deep Dive into Building Streaming Applications with Apache PulsarApacheCon2022_Deep Dive into Building Streaming Applications with Apache Pulsar
ApacheCon2022_Deep Dive into Building Streaming Applications with Apache Pulsar
Timothy Spann175 views
Conf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices by Timothy Spann
Conf42 Python_ ML Enhanced Event Streaming Apps with Python MicroservicesConf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices
Conf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices
Timothy Spann443 views

More from Sarah Mount

Revelation pyconuk2016 by
Revelation pyconuk2016Revelation pyconuk2016
Revelation pyconuk2016Sarah Mount
169 views39 slides
Jatrobot - real-time farm monitoring by
Jatrobot - real-time farm monitoringJatrobot - real-time farm monitoring
Jatrobot - real-time farm monitoringSarah Mount
366 views5 slides
Scala - just good for Java shops? by
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?Sarah Mount
5.4K views18 slides
Mining python-software-pyconuk13 by
Mining python-software-pyconuk13Mining python-software-pyconuk13
Mining python-software-pyconuk13Sarah Mount
6.6K views22 slides
Marvin the paranoid laptop by his owner snim2 by
Marvin the paranoid laptop by his owner snim2Marvin the paranoid laptop by his owner snim2
Marvin the paranoid laptop by his owner snim2Sarah Mount
1.2K views10 slides
Europython lightening talk_on_open_ihm by
Europython lightening talk_on_open_ihmEuropython lightening talk_on_open_ihm
Europython lightening talk_on_open_ihmSarah Mount
703 views6 slides

More from Sarah Mount(6)

Revelation pyconuk2016 by Sarah Mount
Revelation pyconuk2016Revelation pyconuk2016
Revelation pyconuk2016
Sarah Mount169 views
Jatrobot - real-time farm monitoring by Sarah Mount
Jatrobot - real-time farm monitoringJatrobot - real-time farm monitoring
Jatrobot - real-time farm monitoring
Sarah Mount366 views
Scala - just good for Java shops? by Sarah Mount
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?
Sarah Mount5.4K views
Mining python-software-pyconuk13 by Sarah Mount
Mining python-software-pyconuk13Mining python-software-pyconuk13
Mining python-software-pyconuk13
Sarah Mount6.6K views
Marvin the paranoid laptop by his owner snim2 by Sarah Mount
Marvin the paranoid laptop by his owner snim2Marvin the paranoid laptop by his owner snim2
Marvin the paranoid laptop by his owner snim2
Sarah Mount1.2K views
Europython lightening talk_on_open_ihm by Sarah Mount
Europython lightening talk_on_open_ihmEuropython lightening talk_on_open_ihm
Europython lightening talk_on_open_ihm
Sarah Mount703 views

Recently uploaded

Democratising digital commerce in India-Report by
Democratising digital commerce in India-ReportDemocratising digital commerce in India-Report
Democratising digital commerce in India-ReportKapil Khandelwal (KK)
20 views161 slides
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe by
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe
2024: A Travel Odyssey The Role of Generative AI in the Tourism UniverseSimone Puorto
13 views61 slides
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...James Anderson
126 views32 slides
Five Things You SHOULD Know About Postman by
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About PostmanPostman
38 views43 slides
Future of Indian ConsumerTech by
Future of Indian ConsumerTechFuture of Indian ConsumerTech
Future of Indian ConsumerTechKapil Khandelwal (KK)
24 views68 slides
GDSC CTU First Meeting Party by
GDSC CTU First Meeting PartyGDSC CTU First Meeting Party
GDSC CTU First Meeting PartyNational Yang Ming Chiao Tung University
11 views25 slides

Recently uploaded(20)

2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe by Simone Puorto
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe
Simone Puorto13 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson126 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman38 views
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays17 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by The Digital Insurer
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn26 views
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by Dr. Jimmy Schwarzkopf
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software317 views
Unit 1_Lecture 2_Physical Design of IoT.pdf by StephenTec
Unit 1_Lecture 2_Physical Design of IoT.pdfUnit 1_Lecture 2_Physical Design of IoT.pdf
Unit 1_Lecture 2_Physical Design of IoT.pdf
StephenTec15 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays33 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views

python-csp: bringing OCCAM to Python

  • 1. Ancient history python-csp: features and idioms Using built-in processes Future challenges python-csp: bringing OCCAM to Python Sarah Mount Europython 2010 Sarah Mount python-csp: bringing OCCAM to Python
  • 2. Ancient history python-csp: features and idioms Using built-in processes Future challenges 1 Ancient history 2 python-csp: features and idioms Process creation Parallel, sequential and repeated processes Communication via channel read / writes More channels: ALTing and choice More channels: poisoning Producer / consumer or worker / farmer models 3 Using built-in processes 4 Future challenges Sarah Mount python-csp: bringing OCCAM to Python
  • 3. Ancient history python-csp: features and idioms Using built-in processes Future challenges This talk. . . is all about the python-csp project. There is a similar project called PyCSP, these will merge over the next few months. Current code base is here: http://code.google.com/p/python-csp Please do contribute bug fixes / issues / code / docs / rants / . . . Sarah Mount python-csp: bringing OCCAM to Python
  • 4. Ancient history python-csp: features and idioms Using built-in processes Future challenges INMOS B0042 Board c David May The Transputer was the original “multicore” machine and was built to run the OCCAM language – a realisation of CSP. http://www.cs.bris.ac.uk/~dave/transputer.html Sarah Mount python-csp: bringing OCCAM to Python
  • 5. Ancient history python-csp: features and idioms Using built-in processes Future challenges Tony Hoare Invented CSP, OCCAM, Quicksort and other baddass stuff. You may remember him from such talks as his Europython 2009 Keynote. Sarah Mount python-csp: bringing OCCAM to Python
  • 6. Ancient history python-csp: features and idioms Using built-in processes Future challenges OCCAM OCCAM lives on in it’s current implementation as OCCAM-π http://www.cs.kent.ac.uk/projects/ofa/kroc/ It can run on Lego bricks, Arduino boards and other things. Like Python it uses indentation to demark scope. Unlike Python OCCAM is MOSTLY IN UPPERCASE. Sarah Mount python-csp: bringing OCCAM to Python
  • 7. Ancient history python-csp: features and idioms Using built-in processes Future challenges CSP and message passing This next bit will be revision for many of you! Sarah Mount python-csp: bringing OCCAM to Python
  • 8. Ancient history python-csp: features and idioms Using built-in processes Future challenges Most of us think of programs like this http://xkcd.com/205/ Sarah Mount python-csp: bringing OCCAM to Python
  • 9. Ancient history python-csp: features and idioms Using built-in processes Future challenges This is the old “standard” model! Multicore will soon take over the world (if it hasn’t already). Shared memory concurrency / parallelism is hard: Race hazards Deadlock Livelock Tracking which lock goes with which data and what order locks should be used. Operating Systems are old news! Sarah Mount python-csp: bringing OCCAM to Python
  • 10. Ancient history python-csp: features and idioms Using built-in processes Future challenges Message passing Definition In message passing concurrency (or parallelism) “everything” is a process and no data is shared between processes. Instead, data is “passed” between channels which connect processes together. Think: OS processes and UNIX pipes. The actor model (similar to Kamaelia) uses asynchronous channels. Processes write data to a channel and continue execution. CSP (and π-calculus) use synchronous channels, or rendezvous, where a process writing to a channel has to wait until the value has been read by another process before proceeding. Sarah Mount python-csp: bringing OCCAM to Python
  • 11. Ancient history python-csp: features and idioms Using built-in processes Future challenges Process diagram Sarah Mount python-csp: bringing OCCAM to Python
  • 12. Ancient history python-csp: features and idioms Using built-in processes Future challenges Why synchronous channels? Leslie Lamport (1978). Time, clocks, and the ordering of events in a distributed system”. Communications of the ACM 21 (7) Sarah Mount python-csp: bringing OCCAM to Python
  • 13. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Hello World! from c s p . c s p p r o c e s s i m p o r t ∗ @process def h e l l o () : p r i n t ’ H e l l o CSP w o r l d ! ’ hello () . start () Sarah Mount python-csp: bringing OCCAM to Python
  • 14. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Example of process creation: web server @process def s e r v e r ( host , port ) : sock = socket . socket ( . . . ) # S e t up s o c k e t s w h i l e True : conn , a d d r = s o c k . a c c e p t ( ) r e q u e s t = conn . r e c v ( 4 0 9 6 ) . s t r i p ( ) i f r e q u e s t . s t a r t s w i t h ( ’GET ’ ) : ok ( r e q u e s t , conn ) . s t a r t ( ) else : n o t f o u n d ( r e q u e s t , conn ) . s t a r t ( ) Sarah Mount python-csp: bringing OCCAM to Python
  • 15. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Example of process creation: web server @process d e f n o t f o u n d ( r e q u e s t , conn ) : page = ’<h1>F i l e n o t found </h1> ’ conn . s e n d ( r e s p o n s e ( 4 0 4 , ’ Not Found ’ , page ) ) conn . shutdown ( s o c k e t . SHUT RDWR) conn . c l o s e ( ) return Sarah Mount python-csp: bringing OCCAM to Python
  • 16. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Combining processes: in parallel # With a Par o b j e c t : Par ( p1 , p2 , p3 , . . . pn ) . s t a r t ( ) # With s y n t a c t i c s u g a r : p1 // ( p2 , p3 , . . . pn ) # RHS must be a s e q u e n c e t y p e . Sarah Mount python-csp: bringing OCCAM to Python
  • 17. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Combining processes: repeating a process sequentially @process def h e l l o () : p r i n t ’ H e l l o CSP w o r l d ! ’ if name == ’ m a i n ’ : hello () ∗ 3 2 ∗ hello () Sarah Mount python-csp: bringing OCCAM to Python
  • 18. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Channels @process d e f c l i e n t ( chan ) : p r i n t chan . r e a d ( ) @process d e f s e r v e r ( chan ) : chan . w r i t e ( ’ H e l l o CSP w o r l d ! ’ ) if name == ’ m a i n ’ : ch = C h a n n e l ( ) Par ( c l i e n t ( ch ) , s e r v e r ( ch ) ) . s t a r t ( ) Sarah Mount python-csp: bringing OCCAM to Python
  • 19. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models What you need to know about channels Channels are currently UNIX pipes This will likely change Channel rendezvous is “slow” compared with JCSP: 200µ s vs 16µ s This will be fixed by having channels written in C Because channels are, at the OS level, open “files”, there can only be a limited number of them (around 300 on my machine) This makes me sad :-( Sarah Mount python-csp: bringing OCCAM to Python
  • 20. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Message passing an responsiveness This isn’t about speed. Sometimes, you have several channels and reading from them all round-robin may reduce responsiveness if one channel read blocks for a long time. This is BAD: @process def foo ( channels ) : f o r chan i n c h a n n e l s : p r i n t chan . r e a d ( ) Sarah Mount python-csp: bringing OCCAM to Python
  • 21. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models ALTing and choice Definition ALTing, or non-deterministic choice, selects a channel to read from from those channels which are ready to read from. You can do this with (only) two channels: @process d e f f o o ( c1 , c2 ) : p r i n t c1 | c2 p r i n t c1 | c2 Sarah Mount python-csp: bringing OCCAM to Python
  • 22. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models ALTing proper (many channels) @process d e f f o o ( c1 , c2 , c3 , c4 , c5 ) : a l t = A l t ( c1 , c2 , c3 , c4 , c5 ) # Choose random a v a i l a b l e o f f e r print alt . se le ct () # Avoid l a s t s e l e c t e d channel print alt . f a i r s e l e c t () # Choose c h a n n e l w i t h l o w e s t i n d e x print alt . p r i s e l e c t () Sarah Mount python-csp: bringing OCCAM to Python
  • 23. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Syntactic sugar for ALTing @process d e f f o o ( c1 , c2 , c3 , c4 , c5 ) : gen = A l t ( c1 , c2 , c3 , c4 , c5 ) p r i n t gen . n e x t ( ) p r i n t gen . n e x t ( ) p r i n t gen . n e x t ( ) p r i n t gen . n e x t ( ) p r i n t gen . n e x t ( ) Sarah Mount python-csp: bringing OCCAM to Python
  • 24. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models ALTing: when you really, really must return right now @process d e f f o o ( c1 , c2 , c3 ) : # Skip () w i l l / always / complete # a read immediately gen = A l t ( c1 , c2 , c3 , S k i p ( ) ) p r i n t gen . n e x t ( ) ... Sarah Mount python-csp: bringing OCCAM to Python
  • 25. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models Channel poisoning Definition Idiomatically in this style of programming most processes contain infinite loops. Poisoning a channel asks all processes connected to that channel to shut down automatically. Each process which has a poisoned channel will automatically poison any other channels it is connected to. This way, a whole graph of connected processes can be terminated, avoiding race conditions. @process def foo ( channel ) : ... channel . poison () Sarah Mount python-csp: bringing OCCAM to Python
  • 26. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models A bigger example: Mandelbrot generator Sarah Mount python-csp: bringing OCCAM to Python
  • 27. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models @process d e f main ( IMSIZE ) : channels = [ ] # One p r o d u c e r + c h a n n e l f o r e a c h image column . f o r x i n r a n g e ( IMSIZE [ 0 ] ) : c h a n n e l s . append ( C h a n n e l ( ) ) p r o c e s s e s . append ( m a n d e l b r o t ( x , . . . channels [ x ]) ) p r o c e s s e s . i n s e r t ( 0 , consume ( IMSIZE , channels ) ) mandel = Par ( ∗ p r o c e s s e s ) mandel . s t a r t ( ) Sarah Mount python-csp: bringing OCCAM to Python
  • 28. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models @process def mandelbrot ( xcoord , cout ) : # Do some maths h e r e cout . w r i t e ( xcoord , column data ) Sarah Mount python-csp: bringing OCCAM to Python
  • 29. Process creation Ancient history Parallel, sequential and repeated processes python-csp: features and idioms Communication via channel read / writes Using built-in processes More channels: ALTing and choice Future challenges More channels: poisoning Producer / consumer or worker / farmer models @process d e f consumer ( c i n s ) : # I n i t i a l i s e PyGame . . . gen = l e n ( c i n s ) ∗ A l t ( ∗ c i n s ) f o r i in range ( len ( c i n s ) ) : x c o o r d , column = gen . n e x t ( ) # B l i t t h a t column t o s c r e e n f o r e v e n t i n pygame . e v e n t . g e t ( ) : i f e v e n t . t y p e == pygame . QUIT : for channel in cins : channel . poison () pygame . q u i t ( ) Sarah Mount python-csp: bringing OCCAM to Python
  • 30. Ancient history python-csp: features and idioms Using built-in processes Future challenges Oscilloscope Sarah Mount python-csp: bringing OCCAM to Python
  • 31. Ancient history python-csp: features and idioms Using built-in processes Future challenges @process def O s c i ll o s c o p e ( inchan ) : # I n i t i a l i s e PyGame ydata = [ ] w h i l e n o t QUIT : y d a t a . append ( i n c h a n . r e a d ( ) ) y d a t a . pop ( 0 ) # B l i t data to s c r e e n . . . # Deal with e v e n t s Sarah Mount python-csp: bringing OCCAM to Python
  • 32. Ancient history python-csp: features and idioms Using built-in processes Future challenges from c s p . b u i l t i n s i m p o r t S i n def t r a c e s i n () : chans = Channel ( ) , Channel ( ) Par ( G e n e r a t e F l o a t s ( c h a n s [ 0 ] ) , Sin ( chans [ 0 ] , chans [ 1 ] ) , O s c i l l o s c o p e ( chans [ 1 ] ) ) . s t a r t () return Sarah Mount python-csp: bringing OCCAM to Python
  • 33. Ancient history python-csp: features and idioms Using built-in processes Future challenges Process diagram Sarah Mount python-csp: bringing OCCAM to Python
  • 34. Ancient history python-csp: features and idioms Using built-in processes Future challenges def trace mux () : chans = [ Channel ( ) f o r i i n range (6) ] p a r = Par ( G e n e r a t e F l o a t s ( c h a n s [ 0 ] ) , Delta2 ( chans [ 0 ] , chans [ 1 ] , chans [ 2 ] ) , Cos ( c h a n s [ 1 ] , c h a n s [ 3 ] ) , Sin ( chans [ 2 ] , chans [ 4 ] ) , Mux2 ( c h a n s [ 3 ] , c h a n s [ 4 ] , chans [ 5 ] ) , O s c i l l o s c o p e ( chans [ 5 ] ) ) par . s t a r t () Sarah Mount python-csp: bringing OCCAM to Python
  • 35. Ancient history python-csp: features and idioms Using built-in processes Future challenges Wiring Sarah Mount python-csp: bringing OCCAM to Python