python-csp v0.1 - message-passing concurrency in Python

Latest PyPI version Number of PyPI downloads Current state of tests on continuous integration server

python-csp adds C.A.R. (Tony) Hoare’s Communicating Sequential Processes to Python:

>>> @process
... def writer(channel, n):
...      for i in xrange(n):
...              channel.write(i)
...      channel.poison()
...      return
...
>>> @process
... def reader(channel):
...      while True:
...              print channel.read()
...
>>> # Run the two processes in parallel
>>> # connected by a channel
>>> chan = Channel()
>>> Par(reader(chan), writer(chan, 5)).start()
0
1
2
3
4
>>>

What is CSP?

Concurrent and parallel programming are generally seen as “difficult” tasks. This is partly because most people have a mental model of how computers work which is fundamentally sequential, and partly because concurrent programs are susceptible to errors such as race hazards, deadlocks, and so on.

CSP stands for Communicating Sequential Processes, which is a framework for writing concurrent or program via message passing. The advantage of message passing is that it makes race hazards impossible and provides a model of concurrency that is much easier to think about and more fun to program with.

Why CSP for Python?

Python currently has some support for threaded concurrency via the thread and threading module, support for parallel programming and the processing module. Threads are very low level, difficult to work with and cannot take advantage of multicore architectures that are now becoming commonplace. The processing module provides good support for process and thread-safe data structures but neither provide support for the message passing paradigm of programming and the sorts of constructs common to CSP style languages (such as OCCAM, OCCAM-pi, JCSP, C++CSP and so on). python-csp is design to plug this gap and also to provide an idiomatically _pythonic_ implementation of the ideas in CSP.

Installation

python-csp can be installed using PIP (PIP Installs Python):

$ sudo pip install python-csp

or from a source distribution using setup.py:

$ sudo python setup.py install

python-csp lives on GitHub. If you want a copy of the source code you can clone it directly:

$ git clone git://github.com/snim2/python-csp.git

Then install python-csp using the usual Python setup protocol:

$ cd python-csp
$ sudo python setup.py install

Tutorial

Intermediate

  • Part 6 - Using different channel types

Advanced

  • Part 9 – Debugging python-csp programs with extra tool support
  • Part 10 – Reactive (or dataflow) programming with python-csp

Further documentation on python-csp

    1. Mount, M. Hammoudeh, S. Wilson, R. Newman (2009) CSP as a Domain-Specific Language Embedded in Python and Jython. In Proceedings of Communicating Process Architectures 2009. Eindoven, Netherlands. 1st – 4th November 2009. Published IOS Press.

See here for PDF copies of peer-reviewed papers.

Other CSP resources

Other concurrency packages for Python

  • PyCSP is another CSP library for Python, and somewhat similar to python-csp. In the medium term it is intended that PyCSP and python-csp will merge.
  • Trellis reactive programming for Python
  • STM software transactional memory for Python
  • Kamaelia message passing concurrency using asynchronous channels
  • Twisted event-driven Python programming
  • Multiprocessing this is the best support in the current standard library for running forked processes.

Indices and tables