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 ...
随机推荐
- Linux:查看进程运行时间
查看进程运行时间 应用ps命令,加选项-Ao,加参数,最后过滤. ps -Ao pid,tty,user,stime,etime,comm,args| grep firefox pid:进程ID tt ...
- Python学习(001)--计算机基础
操作系统发展历史 操作系统并不是与计算机硬件一起诞生的,它是在人们使用计算机的过程中,为了满足两大需求:提高资源利用率.增强计算机系统性能,伴随着计算机技术本身及其应用的日益发展,而逐步地形成和完善起 ...
- Ubuntu16.04怎样安装Python3.6
Ubuntu16.04默认安装了Python2.7和3.5 请注意,系统自带的python千万不能卸载! 输入命令python
- Excel 设置标题栏
1. 选中列表标题行, 可以设置字体居中显示,并放大字体以表示这是标题栏. 2. 选中列表第一数据行,即列表标题行下一行,选择View > Freeze Panes.
- vim 程序编辑器
基本上vi共分为三种模式,分别是一般模式.编辑模式与指令列命令模式.vi 三种模式的相互关系如下图: vim的暂存档.救援回复与开启时的警告信息当我们在使用vim编辑时,vim会在与被编辑的档案的目录 ...
- Largest Submatrix of All 1’s
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we m ...
- codeforces315Div1 B Symmetric and Transitive
http://codeforces.com/contest/568/problem/B 题意就是给一个有n个元素的集合,现在需要求有多少个A的二元关系p,使得p是对称的,是传递的,但不是自反的. 首先 ...
- RPC好,还是RESTful好?
看到知乎上有这样一个问题 WEB开发中,使用JSON-RPC好,还是RESTful API好? 还有其他优秀的推荐方案吗? -------------------------------------- ...
- MySQL--修改表字段
##========================================================================## ## 修改表字段 ## CHANGE和MODI ...
- C#中Task的使用简单总结
Task在并行计算中的作用很凸显,但是他的使用却有点小复杂,下面是任务的一些基本使用说明(转载与总结于多篇文章) 简单点说说吧! 创建 Task 创建Task有两种方式,一种是使用构造函数创建,另一种 ...