Python 并發編程庫:goless
使用 goless 庫,你可以用 Python 語言編寫 Go 語言風格的并發程序。goless 提供了 channels、select 和 gooutines 的函數,允許你使用 Go 語言漂亮和優雅的并發編程模型,但是以你習慣的 Python 方式。goless 基于 gevent、PyPy 或者 Stackless Python 構建,可用于 PyPy、CPython 和 Stackless Python 解釋器,支持 Python 2.6 到 3.4
示例代碼:
"""
A really simple example to use when demonstrating goless.
"""
from __future__ import print_function
import goless
def simple():
channel = goless.chan()
def goroutine():
while True:
value = channel.recv()
channel.send(value ** 2)
goless.go(goroutine)
for i in range(2, 5):
channel.send(i)
squared = channel.recv()
print('%s squared is %s' % (i, squared))
# Output:
# 2 squared is 4
# 3 squared is 9
# 4 squared is 16
if __name__ == '__main__':
simple()
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!