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数据库做关联一般用id还是用户名2. linux运维 - python远程控制windows如何实现3. thinkPHP5中获取数据库数据后默认选中下拉框的值,传递到后台消失不见。有图有代码,希望有人帮忙4. python小白 关于类里面的方法获取变量失败的问题5. python - 如何对列表中的列表进行频率统计?6. javascript - 如何用最快的速度C#或Python开发一个桌面应用程序来访问我的网站?7. Python2中code.co_kwonlyargcount的等效写法8. django - Python error: [Errno 99] Cannot assign requested address9. python小白,关于函数问题10. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?
