CSP-style concurrency for Python
Aiochan is a library written to bring the wonderful idiom of
CSP-style concurrency to python. The implementation
is based on the battle-tested Clojure library core.async, while the API is
carefully crafted to feel as pythonic as possible.
pip3 install aiochan
Read the beginner-friendly tutorial that starts from the
basics. Or if you are already experienced with golang or Clojure’s
core.async, start with the
quick introduction and then dive into the
API documentation.
The quick introduction and the
beginner-friendly tutorial can both be run in jupyter
notebooks, online in binders if you want (just look for
at the top of each tutorial).
In addition to the introduction and the
tutorial, we have the
complete set of examples from Rob Pike’s
Go concurrency patterns translated into aiochan. Also, here is a
solution to the classical
dining philosophers problem.
We are just starting out, but we will try to answer aiochan-related questions on
stackoverflow as quickly as possible.
File an issue, or if you think you can solve it, a pull request is even
better.
aiochan
is definitely not a toy and we do use it in production, mainly in the two following scenarios:
It is our ‘hello world’ example:
import aiochan as ac
async def blue_python(c):
while True:
# do some hard work
product = "a product made by the blue python"
await c.put(product)
async def yellow_python(c):
while True:
result = await c.get()
# use result to do amazing things
print("A yellow python has received", result)
async def main():
c = ac.Chan()
for _ in range(3):
ac.go(blue_python(c))
for _ in range(3):
ac.go(yellow_python(c))
in other words, it is a 3-fan-in on top of a 3-fan-out. If you run it, you will have an endless stream ofA yellow python has received a product made by the blue python
.
If you have no idea what this is, read the tutorial.