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. java - mybatis怎么实现在数据库中有就修改,没有就添加2. MYSQL新建用户设置可以远程访问的问题3. $fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题4. python - 求一个在def中可以实现调用本def满足特定条件continue效果的方法(标题说不太清楚,请见题内描述)5. angular.js - angularjs的自定义过滤器如何给文字加颜色?6. javascript - 用表单提交两个时间段请求后台返回对应数据时出现的一些问题!7. node.js - nodejs和前端JavaScript 字符串处理结果不一样是什么原因?8. mysql - 我的myeclipse一直连显示数据库连接失败,不知道为什么9. mysql - SQL操作时间的函数?10. mysql 为何insert的时候会有lock wait timeout 异常
![$fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题](http://www.haobala.com/attached/image/news/202205/093622cb60.png)