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.sleep
instead: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.Task
as 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.ThreadPoolExecutor
can 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:8888
in both tabs, loadhttp://localhost:8888/?x=1
in one andhttp://localhost:8888/?x=2
in 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 ...
随机推荐
- Java实现责任链模式
责任链模式: 将接受者对象连成一条链,并在该链上传递请求,直到一个几首这对象处理它.通过让更多对象有机会处理请求,避免了请求发送者和接受者之间的耦合. 责任链模式的优缺点: 优点:高内聚,低耦合.业务 ...
- Motorola C118 PCB原理高清图
- LMS算法
一.感知器算法和LMS算法 感知器和自适应线性元件在历史上几乎是同时提出的,并且两者在对权值的调整的算法非常相似.它们都是基于纠错学习规则的学习算法. 感知器算法存在如下问题:不能推广到一般的前向网络 ...
- OK335xS I2C device registe hacking
/*************************************************************************** * OK335xS I2C device re ...
- android中的5大布局
1.线性布局:LinearLayout layout_margin 上下左右的距离分别为 下面图中的orientation表示的是布局中的方向 分别有horizontal表示水平 vertic ...
- MVC的好处 演示
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- cuda+ffmpeg+opengl解码rtsp h264码流多路
Cuda 解码 全尺寸 解码 .全尺寸窗口绘制测试( 分别 测试 视频 文件和 IP 相机 实时视频 ) 1080 p 视屏 文件 全尺寸 解码 全尺寸 显示 72 0p IP 相机 全尺寸 解码 全 ...
- 在 Laravel 5 中使用 Repository 模式实现业务逻辑和数据访问的分离
1.概述 首先需要声明的是设计模式和使用的框架以及语言是无关的,关键是要理解设计模式背后的原则,这样才能不管你用的是什么技术,都能够在实践中实现相应的设计模式. 按照最初提出者的介绍,Reposito ...
- STS或eclipse安装SVN插件(转)
安装sts--SVN插件 简介:sts是与eclipse类似的Java IDE开发工具(不了解的百度) 1.sts菜单栏 help->install New Software 依据大家的版本选择 ...
- vulcanjs 包类型
npm 添加在pacakge.json 文件中的 meteor core 包 由meteor 框架提供的 meteor remote 包 从包服务器加载的,使用username:package 格式组 ...