Frequently Asked Questions
转自:http://www.tornadoweb.org/en/stable/faq.html
Frequently Asked Questions
Why isn’t this example with time.sleep() running in parallel?
Many people’s first foray into Tornado’s concurrency looks something like this:
class BadExampleHandler(RequestHandler):
def get(self):
for i in range(5):
print(i)
time.sleep(1)
Fetch this handler twice at the same time and you’ll see that the second five-second countdown doesn’t start until the first one has completely finished. The reason for this is that time.sleep is ablocking function: it doesn’t allow control to return to the IOLoop so that other handlers can be run.
Of course, time.sleep is really just a placeholder in these examples, the point is to show what happens when something in a handler gets slow. No matter what the real code is doing, to achieve concurrency blocking code must be replaced with non-blocking equivalents. This means one of three things:
Find a coroutine-friendly equivalent. For
time.sleep, usetornado.gen.sleepinstead:class CoroutineSleepHandler(RequestHandler):
@gen.coroutine
def get(self):
for i in range(5):
print(i)
yield gen.sleep(1)When this option is available, it is usually the best approach. See the Tornado wiki for links to asynchronous libraries that may be useful.
Find a callback-based equivalent. Similar to the first option, callback-based libraries are available for many tasks, although they are slightly more complicated to use than a library designed for coroutines. These are typically used with
tornado.gen.Taskas an adapter:class CoroutineTimeoutHandler(RequestHandler):
@gen.coroutine
def get(self):
io_loop = IOLoop.current()
for i in range(5):
print(i)
yield gen.Task(io_loop.add_timeout, io_loop.time() + 1)Again, the Tornado wiki can be useful to find suitable libraries.
Run the blocking code on another thread. When asynchronous libraries are not available,
concurrent.futures.ThreadPoolExecutorcan be used to run any blocking code on another thread. This is a universal solution that can be used for any blocking function whether an asynchronous counterpart exists or not:# `pip install futures` for python2
executor = concurrent.futures.ThreadPoolExecutor(8) class ThreadPoolHandler(RequestHandler):
@gen.coroutine
def get(self):
for i in range(5):
print(i)
yield executor.submit(time.sleep, 1)
See the Asynchronous I/O chapter of the Tornado user’s guide for more on blocking and asynchronous functions.
My code is asynchronous, but it’s not running in parallel in two browser tabs.
Even when a handler is asynchronous and non-blocking, it can be surprisingly tricky to verify this. Browsers will recognize that you are trying to load the same page in two different tabs and delay the second request until the first has finished. To work around this and see that the server is in fact working in parallel, do one of two things:
- Add something to your urls to make them unique. Instead of
http://localhost:8888in both tabs, loadhttp://localhost:8888/?x=1in one andhttp://localhost:8888/?x=2in the other. - Use two different browsers. For example, Firefox will be able to load a url even while that same url is being loaded in a Chrome tab.
Frequently Asked Questions的更多相关文章
- tmux frequently asked questions
tmux frequently asked questions How is tmux different from GNU screen? tmux and GNU screen have ...
- Relinking Oracle Home FAQ ( Frequently Asked Questions) (Doc ID 1467060.1)
In this Document Purpose Questions and Answers 1) What is relinking ? 2) What is relinking ...
- 06 Frequently Asked Questions (FAQ) 常见问题解答 (常见问题)
Frequently Asked Questions (FAQ) Origins 起源 What is the purpose of the project? What is the history ...
- 成员函数指针 C++ FAQ LITE — Frequently Asked Questions
http://www.sunistudio.com/cppfaq/pointers-to-members.html C++ FAQ LITE — Frequently Asked Questions ...
- openvswith Frequently Asked Questions
Open vSwitch <http://openvswitch.org> 参考地址:http://git.openvswitch.org/cgi-bin/gitweb.cgi?p=ope ...
- Kafka Frequently Asked Questions
This is intended to be an easy to understand FAQ on the topic of Kafka. One part is for beginners, o ...
- NFC Forum : Frequently Asked Questions (NFC 论坛:FAQ)
NFC for Business What is the NFC Forum? The NFC Forum is a not-for-profit industry organization whos ...
- 工作笔记20170315-------关于FAQ(Frequently Asked Questions)列表的代码
源自于:http://www.17sucai.com/pins/3288.html (1)FAQ问答列表点击展开收缩文字列表 <ul> <li class="clear ...
- Frequently Asked Questions - P-thresholds
Source: http://mindhive.mit.edu/book/export/html 1. What is the multiple-comparison problem? What is ...
随机推荐
- 逆向路由器固件之敏感信息泄露 Part2
之前的文章中详细介绍了各种解包路由器固件的工具.解包之后就获得了固件中的文件.下一步就是分析文件寻找漏洞了.这次分析的目标是Trendnet路由器,分析的漏洞是一个远程获取路由器权限的漏洞. 初步分析 ...
- RSA签名验证无法通过,检查以下部分
RSA签名验证无法通过,检查以下部分:1.是否和上游交换公钥,提交给上游的公钥是否配置正确并生效2.检查加密方式是SHA1还是MD5,是否跟上游一致3.上游采用的是否是base64SafeUrl的方式 ...
- centos下tomcat启动卡死
遇到好几次了,总是忘记哪个位置~!!! tomcat启动时查看日志会卡在一个位置很长时间,如下 28-Aug-2018 22:56:55.216 INFO [localhost-startStop-1 ...
- docker-compose工具
环境:CentOS7.0 安装版本:1.3.2 参考资料:https://docs.docker.com/compose/install/ 安装方式: [root@Docker ~]#curl -L ...
- Win7不能用鼠标双击运行jar文件怎么办?
Java应用程序jar文件可以由 JVM(Java虚拟机)直接执行,只要操作系统安装了JVM便可以运行作为Java应用程序的jar文件,其跨平台特性使得很多工具软件都用jar方式来部署分发,比如用于H ...
- PNG文件结构
对于一个PNG文件来说,其文件头总是由位固定的字节来描述的,HEX: 89 50 4E 47 0D 0A 1A 0A 使用ultra打开一个png图片,结果如下: 其中第一个字节0x89超出了ASCI ...
- 《DSP using MATLAB》Problem 4.13
代码: %% ---------------------------------------------------------------------------- %% Output Info a ...
- gem install没有反应 解决办法
在虚拟机上执行gem install redis 没有反应: 百度方法如下: #移除官方镜像,增加淘宝镜像 gem sources --add https://ruby.taobao.org/ --r ...
- dbt 包的构建
dbt的包是一种可以复用的代码,可以方便进行模型的共享 创建一个包 和普通的dbt 项目类似 初始化(init) dbt init [packagename] 目录结构 文件: README.md d ...
- nyoj 素数距离
素数距离问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度 ...