Matt Joiner Matt Joiner - 1 year ago 38
Python Question

CSP Channels with native threading in Python

I'm looking for an implementation of CSP channels on top of native threads in Python. I've seen a few libraries about but they include everything but the kitchen sink.

Specifically, I'm looking for the ability to wait for the first of a set of send and receive operations on multiple channels and either have the results of the first completed operation returned to me, or a callback invoked.

Here are a few related links for context:

Answer Source

Yes, my library python-csp has all of these. You can get the library here:

Here's a simple example with channels and ALTing (also called non-deterministic selection):

>>> @process
... def send_msg(chan, msg):
...     chan.write(msg)
>>> @process
... def alt_example(chan1, chan2):
...     alt = Alt(chan1, chan2)
...     print
...     print
>>> c1, c2 = Channel(), Channel()
>>> Par(send_msg(c1, 'yes'), send_msg(c2, 'no'), alt_example(c1, c2)).start()

I'm in the middle of refactoring the internals and cleaning things up, so keep an eye out for a release pretty soon or feel free to email me offline about it if you wish.