Python 3中新增了一个并行计算的库 asyncio ,还有个比较文艺的名称 tulip 郁金香。
在Python 3.4中直接集成了这个组件, 使用这个组件最低要求Python 3.3。
下面是这个库的官方文档的翻译:
asyncio - 异步IO、 事件循环 、协程和任务
(原文地址:https://docs.python.org/3/library/asyncio.html#module-asyncio)
New in version 3.4. 适用3.4版
Source code: Lib/asyncio/ 源码地址
This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives. Here is a more detailed list of the package contents:
此模块提供的架构,可以使用协程编写单线程并发程序,通过socket或其他资源的多路IO访问,运行网络客户端和服务端,以及相关的基本功能。下面是这个包的详细内容:
a pluggable event loop with various system-specific implementations;
插件模式的事件循环,支持多种系统标准实现;
transport and protocol abstractions (similar to those in Twisted);
传输和协议抽象(类似Twisted中);
concrete support for TCP, UDP, SSL, subprocess pipes, delayed calls, and others (some may be system-dependent);
基础功能:支持TCP、UDP、SSL、紫禁城管道、延迟调用及其他的依赖于系统的功能
a Future class that mimics the one in the concurrent.futures module, but adapted for use with the event loop;
Future类模拟concurrent.futures模块,但是更适合用于事件循环;
coroutines and tasks based on yield from (PEP 380), to help write concurrent code in a sequential fashion;
基于yield from的协程和任务,利于编写现代风格的并行代码;
cancellation support for Futures and coroutines;
取消操作可以支持Futures和协程;
synchronization primitives for use between coroutines in a single thread, mimicking those in the threading module;
同步化基元可以用于协程和单个进程之间,模拟线程模块;
an interface for passing work off to a threadpool, for times when you absolutely, positively have to use a library that makes blocking I/O calls.
一个接口可以用于传递work到在线程池,用于某些时候确实需要用到阻塞模式IO调用的库。