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. mysql儲存json錯誤2. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實現存在即更新應該使用哪個標簽?3. 哭遼 求大佬解答 控制器的join方法怎么轉模型方法4. 怎么php怎么通過數組顯示sql查詢結果呢,查詢結果有多條,如圖。5. sql語句 - 如何在mysql中批量添加用戶?6. mysql - 表名稱前綴到底有啥用?7. mysql - 怎么生成這個sql表?8. Navicat for mysql 中以json格式儲存的數據存在大量反斜杠,如何去除?9. 編輯成功不顯示彈窗10. mysql - 數據庫表中,兩個表互為外鍵參考如何解決
