Summary about python3.x asynchronous concept
以下範例節錄自 TP (yield a tuplip, PyConTW 2014) 連結
因為 producer() 內函 yield 關鍵字,所以是一個產生器,因此可以透過 for 迴圈 iterator 將每個值讀出來。
def producer():
yield 0
yield 1
g = producer()
print(type(g))
for i in g:
print(i)
PEP 342 定義了其架構,在 Python 2.5 實作將 .send() 加入到產生器,因此產生器可以透過 .send(...) 發佈資料,也就讓產生器可以成為協同程序。來看看實際例子。
def simple(a):
print('-> Started: a=', a)
b = yield a
print('-> Received: b=', b)
c = yield a + b
print('-> Received: c=', c)
操作如何用 yield 讓產生器變成協同程序:
>>> s = simple(14)
>>> next(s)
-> Started a = 14
14
>>> s.send(28)
-> Received b = 28
42
>>> s.send(99)
-> Received c = 99
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
- 產生結果,指定 s 為 simple 物件且參數 a = 14
- 呼叫 next() 其實是呼叫 next() 開始
- 將值指定給第一個 yield
- 印出第一個 print
- 產生 a 值並且暫停等候指派給 b
- 傳送 28, 並且指派給 b, 產生 a+b 接著暫停等候指派給 c
- 傳送 99, 指派給 c, 印出 print 接著協同程序終止產生 StopIteration
基本上協同程序就可以讓每個程序做自己的事,完成了再回報。
要達成非同步需要的三個 rules:
- non-blocking sockets
- callback
- event loop
reference:
What Is Async, How Does It Work, and When Should I Use It? (PyCon APAC 2014)
What Is Async, How Does It Work, and When Should I Use It? (NY, 2014)
David Beazley - Python Concurrency From the Ground Up: LIVE! -( PyCon 2015)
Thinking about Concurrency, Raymond Hettinger, Python core developer (PyAU, 2016)
Raymond Hettinger, Keynote on Concurrency (PyBay, 2017)
How Do Python Coroutines Work? (2015)
Coroutine live demo(2015)
Keynote David Beazley - Topics of Interest (Python Asyncio)
asyncio – Asynchronous I/O, event loop, coroutines and tasks, https://docs.python.org/3/library/asyncio.html
PEP 3156, Asynchronous IO Support Rebooted: the "asyncio” Module, http://legacy.python.org/dev/peps/pep-3156/
PEP 380, Syntax for Delegating to a Subgenerator, http://legacy.python.org/dev/peps/pep-0380/
PEP 342, Coroutines via Enhanced Generators, http://legacy.python.org/dev/peps/pep-0342/
PEP 255, Simple Generators, http://legacy.python.org/dev/peps/pep-0255/
asyncio source code, http://hg.python.org/cpython/file/3.4/Lib/asyncio/