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è)高級(jí)語法糖,讓你可以用同步的寫法實(shí)現(xiàn)異步的效果(io多路復(fù)用維護(hù)起狀態(tài)來是相當(dāng)惡心的,現(xiàn)在交由asyncio來幫你維護(hù)了)
相關(guān)文章:
1. android - Windows系統(tǒng)下運(yùn)行react-native App時(shí),報(bào)下面的錯(cuò)誤?2. python - Django分頁和查詢參數(shù)的問題3. 求大神幫我看看是哪里寫錯(cuò)了 感謝細(xì)心解答4. MySQL客戶端吃掉了SQL注解?5. 數(shù)據(jù)庫 - MySQL 單表500W+數(shù)據(jù),查詢超時(shí),如何優(yōu)化呢?6. php自學(xué)從哪里開始?7. mysql - AttributeError: ’module’ object has no attribute ’MatchType’8. 網(wǎng)頁爬蟲 - python爬蟲翻頁問題,請(qǐng)問各位大神我這段代碼怎樣翻頁,還有價(jià)格要登陸后才能看到,應(yīng)該怎么解決9. phpstady在win10上運(yùn)行10. javascript - 圖片能在網(wǎng)站顯示,但控制臺(tái)仍舊報(bào)錯(cuò)403 (Forbidden)
