Occam-like multithreading and communication

Andrew Henshaw andrew_dot_henshaw_at_earthling_dot_net
Thu Oct 26 22:39:51 EDT 2000


I've partially developed a module for Python that implements Occam-like
multithreading  (PAR) and inter-process communication (CHAN) functionality.
For an article discussing why this would be useful, read "Communicating Java
Threads" at

http://www.rt.el.utwente.nl/javapp/cjt/CJT-paper.PDF

I'm curious to see if anybody else would find my module useful.  I believe
that it will greatly simplify my multithreaded Python applications.

I do have a question, but first I will demonstrate usage with a simple
example.

#######################################
from occam import PAR, CHAN

# a simple function to read a value from a channel and print it
def reader(c) :
    a = c.read()
    print a

# a simple function to write a value to a channel
def writer(c, value) :
    c.write(a)

# create the shared channel
c = CHAN()

# wrap the functions in processes and run
par = PAR()
par.PROC(reader, c)
par.PROC(writer, c, some_value)
par.end()

######################################

This example creates two tasks that communicate through a shared
channel, and then runs them.   The par.end() basically does a process join
and waits for both tasks to complete before continuing with the sequential
part of the program.  By the way, the UPPERCASE names are for the
duplication of Occam syntax.

I also have a version that would let me code the bottom part as:

PAR( \
    (reader, c), \
    (writer, c, some_value))

while this looks more Occam-like, I can implement the replicated-PAR
construct with the first, but I don't know how to do it cleanly with the
latter.


Now for my problem.  I would like to implement the capability to easily
create parallel communications that would look like this in Occam:

PAR
    in_chan ? a         -- read from in_chan
    out_chan ! b       -- write to out_chan

So, I'm thinking:

par = PAR()
a = in_chan.read(par)
out_chan.write(b, par)
par.end()

I've written this for the channel write, but for the channel read, I've had
to resort to a kludge.  The problem is that in the statement

a = in_chan.read(par)

I have to perform the assignment to 'a' before the read operation is truly
performed.  In other languages, a package that tries to provide this type of
functionality would handle it like this:

in_chan.read(&a, par)

passing the address of a so that the function could assign the value when it
is ready.
Because of Python's "interesting" assignment methodology, I don't believe
that this is straightforwardly feasible.

My kludge works by:
 - returning a list [None] to a.
 - When the read is actually performed, place the result into the 0th
element of that list.

Then, a[0] will contain the desired result.

Any better suggestions??  I'd appreciate any feedback.

Thanks,
Andy Henshaw


























More information about the Python-list mailing list