- Overview & Motivation
- CSP vs Actor Model
- Core Building Blocks in CSP
- Code examples in PyCSP
- Travelling Salesman Problem in PyCSP
An object oriented language is a language with good support for objects. A concurrency oriented language has good support for concurrency
@process def Process1(): time.sleep(1) # Sleep 1 second print 'process1 exiting' @process def Process2(): time.sleep(2) # Sleep 2 seconds print 'process2 exiting' Parallel(Process1(), Process2()) # Blocks print 'program terminating'
process1 exiting process2 exiting program exiting
@process def Process1(): time.sleep(1) # Sleep 1 second print 'process1 exiting' @process def Process2(): time.sleep(2) # Sleep 2 seconds print 'process2 exiting' Spawn(Process1(), Process2()) # Non-blocking print 'processes started' shutdown() # Blocks print 'program terminating'
processes started process1 exiting process2 exiting program terminating
@process def Process1(chan_out): chan_out('Hello process2') # Blocks print 'process1 exiting' @process def Process2(chan_in): time.sleep(1) # Sleep 1 second print 'reading from channel' msg = chan_in() # Blocks print 'message received: ', msg chan = Channel() Parallel(Process1(chan.writer()), Process2(chan.reader()))
reading from channel message received: Hello process2 process1 exiting
@process def Worker(input_chan): (output_chan, work_func) = input_chan() result = work_func() output_chan(result) def work(): return 2 * 2 input_chan = Channel() Spawn(Worker(input_chan.reader())) result_chan = Channel() msg = (result_chan.writer(), work) # Careful, 'work' is shared reference input_chan.writer()(msg) result = result_chan.reader()() # Get result
@process def Process1(input_chan, output_chan): input_guard = InputGuard(input_chan) output_guard = OutputGuard(output_chan, msg='hello from process1') (chan, msg) = AltSelect(input_guard, output_guard) if chan == input_chan: print 'input read: ', msg elif chan == output_chan: print 'output written' input, output = Channel(), Channel() Spawn(Process1(input.reader(), output.writer())) if random.choice([True, False]): input.writer()('hello process1') else: output.reader()()
@process def Process1(input_chan): guards = [InputGuard(input_chan), SkipGuard()] (chan, msg) = PriSelect(guards) # Always perform skip guard last if chan == input_chan: print 'input read: ', msg else: print 'no input' input = Channel() Spawn(Process1(input.reader())) guard = OutputGuard(input.writer(), msg='hello process1') PriSelect(guard, TimeoutGuard(seconds=1))
Slides available at: http://arild.github.io/csp-presentation