I want find simple async server example.

I have got some function with lot of wait, database transactions ... etc:

def blocking_task(n):

for i in xrange(n):

print i

sleep(1)

return i

I need run it function in separated process without blocking. Is it possible?

解决方案

Tornado is designed to run all your operations in a single thread, but utilize asynchronous I/O to avoid blocking as much as possible. If the DB you're using has asychronous Python bindings (ideally ones geared for Tornado specifically, like Motor for MongoDB or momoko for Postgres), then you'll be able to run your DB queries without blocking the server; no separate processes or threads needed.

To address the exact example you gave, where time.sleep(1) is called, you could use this approach to do it asynchronously via tornado coroutines:

#!/usr/bin/python

import tornado.web

from tornado.ioloop import IOLoop

from tornado import gen

import time

@gen.coroutine

def async_sleep(seconds):

yield gen.Task(IOLoop.instance().add_timeout, time.time() + seconds)

class TestHandler(tornado.web.RequestHandler):

@gen.coroutine

def get(self):

for i in xrange(100):

print i

yield async_sleep(1)

self.write(str(i))

self.finish()

application = tornado.web.Application([

(r"/test", TestHandler),

])

application.listen(9999)

IOLoop.instance().start()

The interesting part is async_sleep. That method is creating an asynchronous Task, which is calling the ioloop.add_timeout method. add_timeout will run a specified callback after a given number of seconds, without blocking the ioloop while waiting for the timeout to expire. It expects two arguments:

add_timeout(deadline, callback) # deadline is the number of seconds to wait, callback is the method to call after deadline.

As you can see in the example above, we're only actually providing one parameter to add_timeout explicitly in the code, which means we end up this this:

add_timeout(time.time() + seconds, ???)

We're not providing the expected callback parameter. In fact, when gen.Task executes add_timeout, it appends a callback keyword argument to the end of the explicitly provided parameters. So this:

yield gen.Task(loop.add_timeout, time.time() + seconds)

Results in this being executed inside gen.Task():

loop.add_timeout(time.time() + seconds, callback=gen.Callback(some_unique_key))

When gen.Callback is executed after the timeout, it signals that the gen.Task is complete, and the program execution will continue on to the next line. This flow is kind of difficult to fully understand, at least at first (it certainly was for me when I first read about it). It'll probably be helpful to read over the Tornado gen module documentation a few times.

python龙卷风框架,龙卷风python的简单异步示例相关推荐

  1. python mvc框架_MVC其实很简单(Django框架)

    Django框架MVC其实很简单 让我们来研究一个简单的例子,通过该实例,你可以分辨出,通过Web框架来实现的功能与之前的方式有何不同. 下面就是通过使用Django来完成以上功能的例子: 首先,我们 ...

  2. 精通python爬虫框架-精通Python爬虫从Scrapy到移动应用(文末福利)

    原标题:精通Python爬虫从Scrapy到移动应用(文末福利) 我能够听到人们的尖叫声:"Appery.io是什么,一个手机应用的专用平台,它和Scrapy有什么关系?"那么,眼 ...

  3. 精通python爬虫框架-精通Python爬虫框架Scrapy.pdf

    作 者 :(美)迪米特里奥斯·考奇斯·劳卡斯(Dimitrios Kouzis Loukas)著:李斌译 出版发行 : 北京:人民邮电出版社 , 2018.02 ISBN号 :978-7-115-47 ...

  4. python分布式计算框架-Parallel Python

    本文翻译自:https://www.parallelpython.com/ 概述 Parallel Python是一个python模块,提供了在SMP(具有多个处理器或核心的操作系统)和群集上并行执行 ...

  5. Python web框架 Tornado(二)异步非阻塞使用以及原理

    原文: http://www.liangxiansen.cn/2018/04/11/tornado/ 作者: 梁先森 稍有改动 Tornado默认是单进程单线程.实时的web特性通常需要为每个用户一个 ...

  6. python程序框架的描述_简单介绍Python下自己编写web框架的一些要点

    在正式开始Web开发前,我们需要编写一个Web框架. 为什么不选择一个现成的Web框架而是自己从头开发呢?我们来考察一下现有的流行的Web框架: Django:一站式开发框架,但不利于定制化: web ...

  7. 《精通Python爬虫框架Scrapy》欢迎来到异步社区!

    欢迎来到异步社区! 异步社区的来历 异步社区(www.epubit.com.cn)是人民邮电出版社旗下IT专业图书旗舰社区,于2015年8月上线运营. 异步社区依托于人民邮电出版社20余年的IT专业优 ...

  8. python学习框架图-Python Scrapy爬虫框架学习

    Scrapy 是用Python实现一个为爬取网站数据.提取结构性数据而编写的应用框架. 一.Scrapy框架简介 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数 ...

  9. 精通python爬虫框架-精通Python爬虫框架Scrapy PDF 中文清晰版

    给大家带来的一篇关于Python爬虫相关的电子书资源,介绍了关于Python.爬虫.框架.Scrapy方面的内容,本书是由人民邮电出版社出版,格式为PDF,资源大小8.6 MB,迪米特里奥斯编写,目前 ...

  10. Python单元测试框架《python 自动化框架 pytest》

    Pytest 简介 pytest 是python 的一种单元测试框架,不python 自带的unittest 测试框架类似,但是比 unittest 框架使用起来更简洁,效率更高.根据pytest 的 ...

最新文章

  1. R语言ggplot2可视化facet间隔设置语法实战
  2. 数据库入门浅析:ASP.NET与MySQL连接
  3. Python面向对象中super用法与MRO机制
  4. c++11 chrono
  5. win10iot c语言,值还是不值?——树莓派3 Win10 IoT系统体验
  6. hive与mysql的数据分区的异同
  7. 【python】TCP协议编程
  8. STM32触摸屏校准数据的存取
  9. 解决VMware下Win10主机和win7虚拟机实现文件共享问题
  10. [转载]Oraclenbsp;grantnbsp;revokenbsp;…
  11. 不得不吐槽的Android PopupWindow的几个痛点(实现带箭头的上下文菜单遇到的坑)...
  12. 实时视频通信技术调研
  13. PHP小马免杀的浅谈[过最新D盾]
  14. tdm的应用计算机,2020计算机考研:TDM时分复用技术备考小知识点
  15. python——正则表达式(re模块)详解
  16. PX4飞行模式-多旋翼
  17. php集成环境和自己配置的区别,php集成环境、php绿色集成环境、php独立安装版环境这三者的区别
  18. blackduck issue fix
  19. 2021及历届国科大高级OS思考题汇总
  20. 德国申根签证,研究生参加国际学术会议商务签出签经验分享

热门文章

  1. 拉普拉斯变换解微分方程
  2. 谷歌浏览器,网页截长图
  3. 排序算法二:二分(折半)插入排序
  4. 分体式水晶头_超6类双屏蔽网线水晶头制作简易教程
  5. CorelDRAW2022矢量绘图软件老牌的矢量图形制作工具
  6. 计算机视觉基础(五)——图像分割/二值化
  7. (期末复习)html元素上下左右置中对齐的三种方法
  8. 荣耀9igoogle模式_创建自己的简单iGoogle小工具
  9. win10任务栏假死问题
  10. L9110H电机驱动模块-FPGA