Asynchronous programming with Tornado
Asynchronous programming can be tricky for beginners, therefore I think it’s useful to iron some basic concepts to avoid common pitfalls. For an explanation about generic asynchronous programming, I recommend you one of the many resourcesonline. I will focus solely on asynchronous programming in Tornado.
From Tornado’s homepage:
FriendFeed’s web server is a relatively simple, non-blocking web server written in Python. The FriendFeed application is written using a web framework that looks a bit like web.py or Google’s webapp, but with additional tools and optimizations to take advantage of the non-blocking web server and tools. Tornado is an open source version of this web server and some of the tools we use most often at FriendFeed. The framework is distinct from most mainstream web server frameworks (and certainly most Python frameworks) because it is non-blocking and reasonably fast. Because it is non-blocking and uses epoll or kqueue, it can handle thousands of simultaneous standing connections, which means the framework is ideal for real-time web services. We built the web server specifically to handle FriendFeed’s real-time features every active user of FriendFeed maintains an open connection to the FriendFeed servers. (For more information on scaling servers to support thousands of clients, see The C10K problem.)
The first step as a beginner is to figure out if you really need to go asynchronous. Asynchronous programming is more complicated that synchronous programming, because, as someone described, it does not fit human brain nicely.
You should use asynchronous programming when your application needs to monitor some resources and react to changes in their state. For example, a web server sitting idle until a request arrives through a socket is an ideal candidate. Or an application that has to execute tasks periodically or delay their execution after some time. The alternative is to use multiple threads (or processes) to control multiple tasks and this model becomes quickly complicated.
The second step is to figure out if you can go asynchronous. Unfortunately in Tornado, not all the tasks can be executed asynchronously.
Tornado is single threaded (in its common usage, although it supports multiple threads in advanced configurations), therefore any “blocking” task will block the whole server. This means that a blocking task will not allow the framework to pick the next task waiting to be processed. The selection of tasks is done by the IOLoop, which, as everything else, runs in the only available thread.
For example, this is a wrong way of using IOLoop:
| import time | |
| from tornado.ioloop import IOLoop | |
| from tornado import gen | |
| def my_function(callback): | |
| print 'do some work' | |
| # Note: this line will block! | |
| time.sleep(1) | |
| callback(123) | |
| @gen.engine | |
| def f(): | |
| print 'start' | |
| # Call my_function and return here as soon as "callback" is called. | |
| # "result" is whatever argument was passed to "callback" in "my_function". | |
| result = yield gen.Task(my_function) | |
| print 'result is', result | |
| IOLoop.instance().stop() | |
| if __name__ == "__main__": | |
| f() | |
| IOLoop.instance().start() |
Note that blocking_call is called correctly, but, being blocking (time.sleep blocks!), it will prevent the execution of the following task (the second call to the same function). Only when the first call will end, the second will be called by IOLoop. Therefore, the output in console is sequential (“sleeping”, “awake!”, “sleeping”, “awake!”).
Compare the same “algorithm”, but using an “asynchronous version” of time.sleep, i.e. add_timeout:
| # Example of non-blocking sleep. | |
| import time | |
| from tornado.ioloop import IOLoop | |
| from tornado import gen | |
| @gen.engine | |
| def f(): | |
| print 'sleeping' | |
| yield gen.Task(IOLoop.instance().add_timeout, time.time() + 1) | |
| print 'awake!' | |
| if __name__ == "__main__": | |
| # Note that now code is executed "concurrently" | |
| IOLoop.instance().add_callback(f) | |
| IOLoop.instance().add_callback(f) | |
| IOLoop.instance().start() |
In this case, the first task will be called, it will print “sleeping” and then it will ask IOLoop to schedule the execution of the rest of the routine after 1 second. IOLoop, having the control again, will fire the second call the function, which will print “sleeping” again and return control to IOLoop. After 1 second IOLoop will carry on where he left with the first function and “awake” will be printed. Finally, the second “awake” will be printed, too. So, the sequence of prints will be: “sleeping”, “sleeping”, “awake!”, “awake!”. The two function calls have been executed concurrently (not in parallel, though!).
So, I hear you asking, “how do I create functions that can be executed asynchronously”? In Tornado, every function that has a “callback” argument can be used with gen.engine.Task. Beware though: being able to use Task does not make the execution asynchronous! There is no magic going on: the function is simply scheduled to execution, executed and whatever is passed tocallback will become the return value of Task. See below:
| import time | |
| from tornado.ioloop import IOLoop | |
| from tornado import gen | |
| def my_function(callback): | |
| print 'do some work' | |
| # Note: this line will block! | |
| time.sleep(1) | |
| callback(123) | |
| @gen.engine | |
| def f(): | |
| print 'start' | |
| # Call my_function and return here as soon as "callback" is called. | |
| # "result" is whatever argument was passed to "callback" in "my_function". | |
| result = yield gen.Task(my_function) | |
| print 'result is', result | |
| IOLoop.instance().stop() | |
| if __name__ == "__main__": | |
| f() | |
| IOLoop.instance().start() |
Most beginners expect to be able to just write: Task(my_func), and automagically execute my_func asynchronously. This is not how Tornado works. This is how Go works! And this is my last remark:
In a function that is going to be used “asynchronously”, only asynchronous libraries should be used.
By this, I mean that blocking calls like time.sleep or urllib2.urlopen or db.query will need to be substituted by their equivalent asynchronous version. For example, IOLoop.add_timeout instead of time.sleep, AsyncHTTPClient.fetchinstead of urllib2.urlopen etc. For DB queries, the situation is more complicated and specific asynchronous drivers to talk to the DB are needed. For example: Motor for MongoDB.
Asynchronous programming with Tornado的更多相关文章
- Async/Await - Best Practices in Asynchronous Programming
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx Figure 1 Summary of Asynchronous Programming ...
- Async/Await - Best Practices in Asynchronous Programming z
These days there’s a wealth of information about the new async and await support in the Microsoft .N ...
- .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)
本文内容 异步编程类型 异步编程模型(APM) 参考资料 首先澄清,异步编程模式(Asynchronous Programming Patterns)与异步编程模型(Asynchronous Prog ...
- HttpWebRequest - Asynchronous Programming Model/Task.Factory.FromAsyc
Posted by Shiv Kumar on 23rd February, 2011 The Asynchronous Programming Model (or APM) has been aro ...
- Parallel Programming AND Asynchronous Programming
https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...
- Asynchronous Programming Patterns
Asynchronous Programming Patterns The .NET Framework provides three patterns for performing asynchro ...
- C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)
https://msdn.microsoft.com/zh-cn/library/mt674882.aspx 侵删 更新于:2015年6月20日 欲获得最新的Visual Studio 2017 RC ...
- Asynchronous programming with async and await (C#)
Asynchronous Programming with async and await (C#) | Microsoft Docs https://docs.microsoft.com/en-us ...
- .Net Core自实现CLR异步编程模式(Asynchronous programming patterns)
最近在看一个线程框架,对.Net的异步编程模型很感兴趣,所以在这里实现CLR定义的异步编程模型,在CLR里有三种异步模式如下,如果不了解的可以详细看MSDN 文档Asynchronous progra ...
随机推荐
- GSM信道分类
GSM是一个数字峰窝无线网络,它采用时分多址(TDMA)技术,在一个网络信道中支持多组通话.时分多址技术将一个GSM信道分为多个时隙(时间段),然后将这些时隙分配给移动电话用户,其中,分配给同一个用户 ...
- Linux 内核链表实现和使用(一阴一阳,太极生两仪~)
0. 概述 学习使用一下 linux 内核链表,在实际开发中我们可以高效的使用该链表帮我们做点事, 链表是Linux 内核中常用的最普通的内建数据结构,链表是一种存放和操作可变数据元 素(常称为节点) ...
- C语言基础:二维数组 分类: iOS学习 c语言基础 2015-06-10 21:42 16人阅读 评论(0) 收藏
二维数组和一位数组类似. 定义: 数据类型 数组名[行][列]={{ },{ }....}; 定义时,一维(行)的长度可以省略,但是二维(列)的长度不可以省略.但是访问时,一定使用双下标. 二维数组的 ...
- Xcode清理垃圾
摘抄自https://blog.csdn.net/hu434587115/article/details/54602449 ~/Library/Developer/Xcode/DerivedData/ ...
- Vue CLI 3 配置兼容IE10
最近做了一个基于Vue的项目,需要兼容IE浏览器,目前实现了打包后可以在IE10以上运行,但是还不支持在运行时兼容IE10及以上. 安装依赖 yarn add --dev @babel/polyfil ...
- Linux:rm:du命令
RM 删除选项rm -r 递归删除,删除目录下所有 删除当前文件下所有文件呢? rm -rf * rm -f 忽略删除提醒 万千从中找到文件删除 ls |grep abc |xargs rm -f 保 ...
- 20155224 2016-2017-2 《Java程序设计》第5周学习总结
20155224 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 Java中的错误都会被打包为对象,可以尝试(try)捕捉(catch)代表错误的对象 ...
- Navicat Premium连接PostgreSQL
连接PostgreSQL时,报错 大致意思:你当前的IP没有连接权限,在文件pg_hba中缺少当前IP的配置 解决:找你的PostgreSQL安装路径,这是我的:C:\Program Files\Po ...
- 牛客练习赛14A(唯一分解定理)
https://www.nowcoder.com/acm/contest/82/A 首先这道题是求1~n的最大约数个数的,首先想到使用唯一分解定理,约数个数=(1+e1)*(1+e2)..(1+en) ...
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo
在上一篇里,我已经建立了一个简单的Web-Demo应用程序.这一篇将记录将此Demo程序改造成一个Web Api应用程序. 一.添加ASP.NET Core MVC包 1. 在project.json ...