您的位置:首頁技術文章
文章詳情頁

linux - Python中aysncio的事件循環是屬于線程還是進程?

瀏覽:111日期:2022-09-09 13:37:54

問題描述

默認的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來幫你維護了)

標簽: Python 編程
相關文章:
国产综合久久一区二区三区