linux - Python中aysncio的事件循環(huán)是屬于線程還是進(jìn)程?
問題描述
默認(rèn)的get_event_loop獲取的loop是基于線程還是進(jìn)程,找了很久的官方文檔才找到這么一句描述,看起來是屬于當(dāng)前線程的?
18.5.2.4. Event loop policies and the default policyFor most users of asyncio, policies never have to be dealt with explicitly, since the default global policy is sufficient. The default policy defines context as the current thread, and manages an event loop per thread that interacts with asyncio. The module-level functions get_event_loop() and set_event_loop() provide convenient access to event loops managed by the default policy.
那么官方文檔提供的另外兩個(gè)獲取事件循環(huán)(下面的pre塊)方法一般用于什么地方?關(guān)于事件循環(huán)嘗試看了源碼但涉及到多路IO復(fù)用問題遂放棄,求各位網(wǎng)友的指點(diǎn)?
loop = asyncio.new_event_loop()asyncio.set_event_loop(loop)
問題解答
回答1:屬于協(xié)程,即用戶態(tài)線程,在單條內(nèi)核線程上分出多個(gè)用戶線程,本質(zhì)還是單線程,算是一個(gè)高級語法糖,讓你可以用同步的寫法實(shí)現(xiàn)異步的效果(io多路復(fù)用維護(hù)起狀態(tài)來是相當(dāng)惡心的,現(xiàn)在交由asyncio來幫你維護(hù)了)
相關(guān)文章:
1. thinkPHP5中獲取數(shù)據(jù)庫數(shù)據(jù)后默認(rèn)選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙2. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時(shí)間會消失是什么情況?3. linux運(yùn)維 - python遠(yuǎn)程控制windows如何實(shí)現(xiàn)4. Python2中code.co_kwonlyargcount的等效寫法5. python - 如何對列表中的列表進(jìn)行頻率統(tǒng)計(jì)?6. django - Python error: [Errno 99] Cannot assign requested address7. mysql數(shù)據(jù)庫做關(guān)聯(lián)一般用id還是用戶名8. javascript - 如何用最快的速度C#或Python開發(fā)一個(gè)桌面應(yīng)用程序來訪問我的網(wǎng)站?9. python小白,關(guān)于函數(shù)問題10. python小白 關(guān)于類里面的方法獲取變量失敗的問題
