CSP based on OS processes

Communicating sequential processes, in Python.

When using CSP Python as a DSL, this module will normally be imported via the statement ‘from csp.cspprocess import *‘.

Copyright (C) Sarah Mount, 2008.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have rceeived a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

class cspprocess.CSPProcess(func, *args, **kwargs)

Implementation of CSP processes. Not intended to be used in client code. Use @process instead.

run()
Called automatically when the L{start} methods is called.
class cspprocess.CSPServer(func, *args, **kwargs)

Implementation of CSP server processes. Not intended to be used in client code. Use @forever instead.

run()
Called automatically when the L{start} methods is called.
class cspprocess.Alt(*args)

CSP select (OCCAM ALT) process.

What should happen if a guard is poisoned?

fair_select()
Select a guard to synchronise with. Do not select the previously selected guard (unless it is the only guard available).
poison()

Poison the last selected guard and unlink from the guard list.

Sets self.last_selected to None.

pri_select()
Select a guard to synchronise with, in order of “priority”. The guard with the lowest index in the L{guards} list has the highest priority.
select()
Randomly select from ready guards.
class cspprocess.Par(*procs, **kwargs)

Run CSP processes in parallel.

start(timeout=None)
Start then synchronize with the execution of parallel processes. Return when all parallel processes have returned.
terminate()
Terminate the execution of this process.
class cspprocess.Seq(*procs)

Run CSP processes sequentially.

start()
Start this process running.
stop()

Terminate the execution of this process.

FIXME: Remove this method.

class cspprocess.Guard

Abstract class to represent CSP guards.

All methods must be overridden in subclasses.

disable()
Roll back from an L{enable} call.
enable()
Prepare for, but do not commit to a synchronisation.
is_selectable()
Should return C{True} if this guard can be selected by an L{Alt}.
poison()
Terminate all processes attached to this guard.
select()
Commit to a synchronisation started by L{enable}.
class cspprocess.Channel

CSP Channel objects.

In python-csp there are two sorts of channel. In JCSP terms these are Any2Any, Alting channels. However, each channel creates an operating system level pipe. Since this is a file object the number of channels a program can create is limited to the maximum number of files the operating system allows to be open at any one time. To avoid this bottleneck use L{FileChannel} objects, which close the file descriptor used for IPC after every read or write operations. Read and write operations are, however, over 20 time slower when performed on L{FileChannel} objects.

Subclasses of C{Channel} must call L{_setup()} in their constructor and override L{put}, L{get}, L{__del__}, L{__getstate__} and L{__setstate__}, the latter two methods for pickling.

disable()

Disable this channel for Alt selection.

MUST be called after L{enable} if this channel is not selected.

enable()

Enable a read for an Alt select.

MUST be called before L{select()} or L{is_selectable()}.

get()
Get a Python object from a process-safe store.
is_selectable()
Test whether Alt can select this channel.
poison()
Poison a channel causing all processes using it to terminate.
put(item)
Put C{item} on a process-safe store.
read()
Read (and return) a Python object from this channel.
resume()
Resume this mobile channel after migrating between processes.
select()
Complete a Channel read for an Alt select.
suspend()
Suspend this mobile channel before migrating between processes.
write(obj)
Write a Python object to this channel.
class cspprocess.FileChannel

Channel objects using files on disk.

C{FileChannel} objects close their files after each read or write operation. The advantage of this is that client code can create as many C{FileChannel} objects as it wishes (unconstrained by the operating system’s maximum number of open files). In return there is a performance hit – reads and writes are around 10 x slower on C{FileChannel} objects compared to L{Channel} objects.

get()
Get a Python object from a process-safe store.
put(item)
Put C{item} on a process-safe store.
resume()
Suspend this mobile channel after migrating between processes.
suspend()
Suspend this mobile channel before migrating between processes.
cspprocess.process(func)

Decorator to turn a function into a CSP process.

Note that the function itself will not be a CSPProcess object, but will generate a CSPProcess object when called.

cspprocess.forever(func)

Decorator to turn a function into a CSP server process.

It is preferable to use this rather than @process, to enable the CSP tracer to terminate correctly and produce a CSP model, or other debugging information.

Indices and tables

Table Of Contents

Previous topic

Welcome to python-csp’s documentation!

Next topic

CSP based on POSIX threads

This Page