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. vim中编辑HTML文件时换行不能缩进2. git - 在web应用分布式部署的情况下怎么进行配置更新3. javascript - Ajax返回json格式之后的数据解析后取出来的数据为undefined?4. 前端 - WebStrom安装了angularjs插件,但是没有语法提示5. 网页爬虫 - 关于Python的编码与解码问题6. css - 文字排版问题,内容都是动态抓出来的,字数不一定。如何对齐啊7. css3 - 微信前端页面遇到的transition过渡动画的bug8. redis - 究竟是选择微信小程序自带的统计工具还是自己开发一个数据统计的代码?9. 微信开放平台 - ios APP能不能打开微信然后通过微信跳转到指定的URL?10. python3.x - 我把3.6的卸载了,也重启了,但是在cmd用python -V指令查看版本时,还是提示下图的python3.6

网公网安备