CSP Channels with native threading in Python
-
14-04-2021 - |
Question
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:
Solution
Yes, my library python-csp has all of these. You can get the library here: https://github.com/futurecore/python-csp
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 alt.select()
... print alt.select()
...
>>> c1, c2 = Channel(), Channel()
>>> Par(send_msg(c1, 'yes'), send_msg(c2, 'no'), alt_example(c1, c2)).start()
yes
no
>>>
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.