linux - Python中aysncio的事件循環是屬于線程還是進程?
問題描述
默認的get_event_loop獲取的loop是基于線程還是進程,找了很久的官方文檔才找到這么一句描述,看起來是屬于當前線程的?
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.
那么官方文檔提供的另外兩個獲取事件循環(下面的pre塊)方法一般用于什么地方?關于事件循環嘗試看了源碼但涉及到多路IO復用問題遂放棄,求各位網友的指點?
loop = asyncio.new_event_loop()asyncio.set_event_loop(loop)
問題解答
回答1:屬于協程,即用戶態線程,在單條內核線程上分出多個用戶線程,本質還是單線程,算是一個高級語法糖,讓你可以用同步的寫法實現異步的效果(io多路復用維護起狀態來是相當惡心的,現在交由asyncio來幫你維護了)
相關文章:
1. php工具中的mysql還是5.1以下的,請問如何才能升級到5.1以上?2. java - 根據月份查詢多個表里的內容怎么實現好?3. 前端 - 關于img父容器的高度會比img的高度多幾像素的問題?4. javascript - 在 model里定義的 引用表模型時,model為undefined。5. atom開始輸入!然后按tab只有空格出現沒有html格式出現6. css3 - 這個右下角折角用css怎么畫出來?7. css3 - 沒明白盒子的height隨width的變化這段css是怎樣實現的?8. python3.x - c++調用python39. javascript - 移動端自適應10. ios - 類似微博首頁,一張圖的時候是如何確定圖大小的?
