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. objective-c - iOS開發支付寶和微信支付完成為什么跳轉到了之前開發的一個app?2. mysql優化 - 關于mysql分區3. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應4. 致命錯誤: Class ’appfacadeTest’ not found5. javascript - ionic2 input autofocus 電腦成功,iOS手機鍵盤不彈出6. python - 管道符和ssh傳文件7. css - 移動端字體設置問題8. css - 求推薦適用于vue2的框架 像bootstrap這種類型的9. html5 - 如何實現帶陰影的不規則容器?10. javascript - 循環嵌套多個promise應該如何實現?
