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. 注冊賬戶文字不能左右分離2. JavaScript事件3. html5 - ElementUI table中el-table-column怎么設置百分比顯示。4. javascript - table列過多,有什么插件可以提供列排序和選擇顯示列的功能5. python - 使用readlines()方法讀取文件內容后,再用for循環遍歷文件與變量匹配時出現疑難?6. javascript - ES6標準入門中let命令提到的for循環打印i是10,如果不用let怎么替換?7. showpassword里的this 是什么意思?代表哪個元素8. 對mysql某個字段監控的功能9. python - 為什么正常輸出中文沒有亂碼,zip函數之后出現中文編程unicode編碼的問題,我是遍歷輸出的啊。10. html - vue項目中用到了elementUI問題
