项目作者: zh217

项目描述 :
CSP-style concurrency for Python
高级语言: Python
项目地址: git://github.com/zh217/aiochan.git
创建时间: 2018-07-31T17:45:22Z
项目社区:https://github.com/zh217/aiochan

开源协议:Apache License 2.0

下载


aiochan

Build Status
Documentation Status
codecov
PyPI version
PyPI version
PyPI status
GitHub license

logo

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.

Why?

  • Doing concurrency in Python was painful
  • asyncio sometimes feels too low-level
  • I am constantly missing capabilities from golang and
    core.async
  • It is much easier to port core.async to Python than to port all those
    wonderful python packages to some other
    language.

What am I getting?

  • Pythonic API that includes everything you’d need for CSP-style
    concurrency programming
  • Works seamlessly with existing asyncio-based libraries
  • Fully tested
  • Fully documented
  • Guaranteed to work with Python 3.5.2 or above and PyPy 3.5 or above
  • Depends only on python’s core libraries, zero external dependencies
  • Proven, efficient implementation based on Clojure’s battle-tested core.async
  • Familiar semantics for users of golang‘s channels and Clojure’s core.async channels
  • Flexible implementation that does not depend on the inner workings of asyncio at all
  • Permissively licensed
  • A beginner-friendly tutorial to get newcomers onboard as
    quickly as possible

How to install?

  1. pip3 install aiochan

How to use?

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.

I want to try it first

The quick introduction and the
beginner-friendly tutorial can both be run in jupyter
notebooks, online in binders if you want (just look for the binder link
at the top of each tutorial).

Examples

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.

I still don’t know how to use it

We are just starting out, but we will try to answer aiochan-related questions on
stackoverflow as quickly as possible.

I found a bug

File an issue, or if you think you can solve it, a pull request is even
better.

Do you use it in production? For what use cases?

aiochan is definitely not a toy and we do use it in production, mainly in the two following scenarios:

  • Complex data-flow in routing. We integrate aiochan with an asyncio-based web server.
    This should be easy to understand.
  • Data-preparation piplelines. We prepare and pre-process data to feed into our machine learning
    algorithms as fast as possible so that our algorithms spend no time waiting for data
    to come in, but no faster than necessary so that we don’t have a memory explosion due to
    data coming in faster than they can be consumed. For this we make heavy use of
    parallel_pipe
    and parallel_pipe_unordered.
    Currently we are not aware of any other library that can completely satisfy this need of ours.

What’s up with the logo?

It is our ‘hello world’ example:

  1. import aiochan as ac
  2. async def blue_python(c):
  3. while True:
  4. # do some hard work
  5. product = "a product made by the blue python"
  6. await c.put(product)
  7. async def yellow_python(c):
  8. while True:
  9. result = await c.get()
  10. # use result to do amazing things
  11. print("A yellow python has received", result)
  12. async def main():
  13. c = ac.Chan()
  14. for _ in range(3):
  15. ac.go(blue_python(c))
  16. for _ in range(3):
  17. 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 of
A yellow python has received a product made by the blue python.

If you have no idea what this is, read the tutorial.