tornado的IOLoop.instance()方法和IOLoop.current()方法区别
在使用tornado时,经常有人疑惑IOLoop.instance()方法和IOLoop.current()方法的区别是什么。
IOLoop.instance()
返回一个全局 IOLoop实例。
大多数应用程序在主线程上运行着一个全局IOLoop,使用IOLoop.instance()方法可以在其他线程上获取这个实例。
IOLoop.current()
返回当前线程的IOLoop,如果IOLoop当前正在运行或已被make_current标记为当前,则返回该实例。如果没有当前IOLoop,默认情况下返回IOLoop.instance(),即返回主线程的IOLoop,如果没有,则进行创建。
一般情况下,当构造异步对象时,你默认应该使用IOLoop.current(),当你在另外一个线程上和主线程进行通信时,使用IOLoop.instance()。
在tornado 5.0之后的版本,instance()已经成为current()的别称,即就是调用instance方法时,实际上调用的是current方法。
贴一下源码
def instance():
return IOLoop.current()
def current(instance=True):
if asyncio is None:
current = getattr(IOLoop._current, "instance", None)
if current is None and instance:
current = IOLoop()
if IOLoop._current.instance is not current:
raise RuntimeError("new IOLoop did not become current")
else:
try:
loop = asyncio.get_event_loop()
except (RuntimeError, AssertionError):
if not instance:
return None
raise
try:
return IOLoop._ioloop_for_asyncio[loop]
except KeyError:
if instance:
from tornado.platform.asyncio import AsyncIOMainLoop
current = AsyncIOMainLoop(make_current=True)
else:
current = None
return current
tornado的IOLoop.instance()方法和IOLoop.current()方法区别的更多相关文章
- $(document).ready()即$()方法和window.onload方法的比较
以浏览器装载文档为例,我们都知道在页面完毕后,浏览器会通过JavaScript为DOM元素添加事件.在常规的JavaScript代码中,通常使用window.onload方法,而在jQuery中,使用 ...
- Server.Transfer方法,Server.Execute方法和Response.Redirect方法有什么异同
(1)Server.Transfer方法: Server.Transfer("m2.aspx");//页面转向(服务器上执行). 服务器停止解析本页,保存此页转向前的数据后,再使页 ...
- java——多线程——单例模式的static方法和非static方法是否是线程安全的?
单例模式的static方法和非static方法是否是线程安全的? 答案是:单例模式的static方法和非static方法是否是线程安全的,与单例模式无关.也就说,如果static方法或者非static ...
- synchronized 修饰在 static方法和非static方法的区别
Java中synchronized用在静态方法和非静态方法上面的区别 在Java中,synchronized是用来表示同步的,我们可以synchronized来修饰一个方法.也可以synchroniz ...
- $(document).ready()方法和window.onload()方法
$(document).ready()方法和window.onload()方法 $(document).ready()方法是JQuery中的方法,他在DOM完全就需时就可以被调用,不必等待这些元素关联 ...
- Html.Partial方法和Html.RenderPartial方法
分布视图 PartialView 一般是功能相对独立的,类似用户控件的视图代码片段,可以被多个视图引用,引用方式如下. 1,Html.Partial方法和Html.RenderPartial方法 静态 ...
- 【转载】C#中double.TryParse方法和double.Parse方法的异同之处
在C#编程过程中,double.TryParse方法和double.Parse方法都可以将字符串string转换为double类型,但两者还是有区别,最重要的区别在于double.TryParse方法 ...
- 【转载】 C#中decimal.TryParse方法和decimal.Parse方法的异同之处
在C#编程过程中,decimal.TryParse方法和decimal.Parse方法都可以将字符串string转换为decimal类型,但两者还是有区别,最重要的区别在于decimal.TryPar ...
- 【转载】C#中float.TryParse方法和float.Parse方法的异同之处
在C#编程过程中,float.TryParse方法和float.Parse方法都可以将字符串string转换为单精度浮点类型float,但两者还是有区别,最重要的区别在于float.TryParse方 ...
随机推荐
- acwing 861. 二分图的最大匹配 模板
地址 https://www.acwing.com/problem/content/description/863/ 给定一个二分图,其中左半部包含n1n1个点(编号1~n1n1),右半部包含n2n ...
- eclipse 的安装
打开eclipse官网 https://www.eclipse.org/ 点击此处 再点击 最后点击下载 然后一路下一步安装即可 添加中文语言包 打开eclipse官网 https://www.ecl ...
- python 打飞机项目 ( 基类封装 )
项目代码 | plane # -*- coding:utf-8 -*- import pygame, time from Plane import Plane from HeroPlane impor ...
- 从零开始的vue学习笔记(六)
混入 混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能.简单解释就是把一个Vue组件中的内容提供给别的组件来用.例子: // 定义一个混入对象 var myMixin ...
- 极客时间-vue开发实战学习(ant-design vue作者)
vue基础 属性 事件 插槽 指令(Directives) 生命周期 底层原理 vue生态 路由管理器vue Router 状态管理器vuex 同构Nuxt vue实战 实战项目-ant-desing ...
- Image 鼠标拖拽与鼠标中键的缩放
一.Image在窗体上拖拽,势必会用到鼠标的三个事件(MouseDown,MouseUp,MouseMove),以左键为例,PictureBox为载体 Point mouseDownPoint = n ...
- ubutu tornado python3.7.5 nginx supervisor 部署web api
环境: 1.Ubuntu 服务器 2.python3.7.5 安装 1.python3.7.5 安装的话还是比较简单,流程大致是./configure ->make && mak ...
- sqlserver 筛选索引(filter index)在使用时需要注意的事项
sqlserver 的筛选索引(filter index)与常规的非筛选索引,加了一定的filter条件,可以按照某些条件对表中的字段进行索引,但是filter 索引在查询 使用上,并不等同于常规的索 ...
- 14. Go 语言编译与工具
Go 语言编译与工具 Go 语言的工具链非常丰富,从获取源码.编译.文档.测试.性能分析,到源码格式化.源码提示.重构工具等应有尽有. 在 Go 语言中可以使用测试框架编写单元测试,使用统一的命令行即 ...
- B站弹幕爬取 / jieba分词 - 全站第一的视频弹幕都在说什么?
前言 本次爬取的视频av号为75993929(11月21的b站榜首),讲的是关于动漫革命机,这是一部超魔幻现实主义动漫(滑稽),有兴趣的可以亲身去感受一下这部魔幻大作. 准备工作 B站弹幕的爬取的接口 ...