前言
我最近都在寫一些Python 3.8的新功能介紹的文章,在自己的項(xiàng)目中也在提前體驗(yàn)新的Python版本。為什么我對(duì)這個(gè)Python 3.8這么有興趣呢?主要是因?yàn)樵赑ython 2停止官方維護(hù)的2020年來臨之前,Python 3.8是最后一個(gè)大版本,雖然還沒有公布Python 3.9的發(fā)布時(shí)間表,但是按過去的經(jīng)驗(yàn),我覺得至少等Python 3.8.4發(fā)布之后才可能發(fā)布Python 3.9.0,那會(huì)應(yīng)該已經(jīng)在2020年年末了。所以大家最近2年的話題都會(huì)是Python 3.8。本周五(2019-05-31)將發(fā)布3.8.0 beta 1,這幾天開發(fā)者們都在抓緊時(shí)間合并代碼趕上Python 3.8最后一班車。這幾天我將陸續(xù)分享幾個(gè)新合并的特性。今天先說 asyncio REPL
REPL
REPL是 Read-Eval-Print Loop 的縮寫,是一種簡單的,交互式的編程環(huán)境:
- Read。獲得用戶輸入
- Eval。對(duì)輸入求值
- Print。打印,輸出求值的結(jié)果
- Loop。循環(huán),可以不斷的重復(fù)Read-Eval-Print
REPL對(duì)于學(xué)習(xí)一門新的編程語言非常有幫助,你可以再這個(gè)交互環(huán)境里面通過輸出快速驗(yàn)證你的理解是不是正確。CPython自帶了一個(gè)這樣的編程環(huán)境:
python Python 3.7.1 (default, Dec 13 2018, 22:28:16) [Clang 10.0.0 (clang-1000.11.45.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def a(): ... return 'A' ... >>> a() 'A'
不過官方自帶的這個(gè)環(huán)境功能非常有限,有經(jīng)驗(yàn)的Python開發(fā)者通常會(huì)使用IPython,我寫的大部分文章里面的代碼都在IPython里面執(zhí)行的, 而且IPython從 7.0開始支持了Async REPL:
ipython defPython 3.7.1 (default, Dec 13 2018, 22:28:16) Type 'copyright', 'credits' or 'license' for more information IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: def a(): ...: return 'A' ...: In [2]: a() Out[2]: 'A' In [3]: import asyncio In [4]: async def b(): ...: await asyncio.sleep(1) ...: return 'B' ...: In [5]: await b() Out[5]: 'B' In [6]: asyncio.run(b()) Out[6]: 'B'
簡單地說,就是在IPython里面可以直接使用await,而不必用 asyncio.run(b()) 。這個(gè)在官方REPL里面是不行的:
python Python 3.7.1 (default, Dec 13 2018, 22:28:16) [Clang 10.0.0 (clang-1000.11.45.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import asyncio >>> async def b(): ... await asyncio.sleep(1) ... return 'B' ... >>> await b() File "", line 1 SyntaxError: 'await' outside function
是的,await只能在異步函數(shù)里面才可以使用。
Python 3.8的asyncio REPL
好消息是官方REPL也與時(shí)俱進(jìn),支持asyncio REPL了。具體細(xì)節(jié)可以看延伸閱讀鏈接1:?
./python.exe -m asyncio asyncio REPL 3.8.0a4+ (heads/master:8cd5165ba0, May 27 2019, 22:28:15) [Clang 10.0.0 (clang-1000.11.45.5)] on darwin Use "await" directly instead of "asyncio.run()". Type "help", "copyright", "credits" or "license" for more information. >>> import asyncio >>> async def b(): ... await asyncio.sleep(1) ... return 'B' ... >>> await b() 'B' >>> async def c(): ... await asyncio.sleep(1) ... return 'C' ... >>> task = asyncio.create_task(c()) >>> await task 'C' >>> await asyncio.sleep(1)
注意激活REPL不是直接輸入python,而是要用 python -m asyncio ,另外那個(gè) import asyncio 是激活REPL時(shí)自動(dòng)幫你輸入的。
延伸閱讀
先別看代碼,看看你能不能實(shí)現(xiàn)這個(gè)功能 :yum:
https://github.com/python/cpython/pull/13472
總結(jié)
以上所述是小編給大家介紹的Python 3.8新特征之a(chǎn)syncio REPL,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
