如何捕捉@tornado.gen.coroutine里的异常
from tornado import gen
from tornado.ioloop import IOLoop @gen.coroutine
def throw(a,b):
try:
a/b
raise gen.Return('hello')
except Exception, e:
pass
@gen.coroutine
def test():
print "i'm ok"
res = yield throw(1,1)
print res #res始终为None
print "here too" test()
IOLoop.instance().start()
res = yield throw(1,1)这里res获取的结果始终为空,因为throw内部用了try...except...,而@gen.coroutine本身就是以抛出异常的形式返回的,所以不管throw函数里的a/b这一句有不有异常,不管调用throw(1,0)还是throw(1,1)返回的都是None,
正确的做法是在外部调用的位置添加try...catch... 即:
@gen.coroutine
def throw(a,b):
a/b
raise gen.Return('hello')
@gen.coroutine
def test():
print "i'm ok"
try:
res = yield throw(1,0)
except Exception, e:
print 'EXCEPTION!', e
print "here too" test()
IOLoop.instance().start()
参考:https://github.com/tornadoweb/tornado/issues/759
如何捕捉@tornado.gen.coroutine里的异常的更多相关文章
- Tornado @tornado.gen.coroutine 与 yield
在使用 Tornado 的过程中产生了以下疑问: 什么时候需要给函数增加 @tornado.gen.coroutine 什么时候调用函数需要 yield @tornado.gen.coroutine ...
- Tornado源码分析系列之一: 化异步为'同步'的Future和gen.coroutine
转自:http://blog.nathon.wang/2015/06/24/tornado-source-insight-01-gen/ 用Tornado也有一段时间,Tornado的文档还是比较匮乏 ...
- 使用tornado的gen.coroutine进行异步编程
在tornado3发布之后,强化了coroutine的概念,在异步编程中,替代了原来的gen.engine, 变成现在的gen.coroutine.这个装饰器本来就是为了简化在tornado中的异步编 ...
- Tornado中gen.coroutine详解
1.gen.coroutine的作用 自动执行生成器 2.Future对象 在介绍异步使用之前,先了解一下Future对象的作用. Future简单可以理解为一个占位符,将来会执行的对象,类似java ...
- tornado.gen 模块解析
转自:http://strawhatfy.github.io/2015/07/22/Tornado.gen/ 引言 注:正文中引用的 Tornado 代码除特别说明外,都默认引用自 Tornado 4 ...
- 理解 tornado.gen
转自:http://blog.xiaogaozi.org/2012/09/21/understanding-tornado-dot-gen/ 理解 tornado.gen SEP 21ST, 2012 ...
- 理解Python语言里的异常(Exception)
Exception is as a sort of structured "super go to".异常是一种结构化的"超级goto". 作为一个数十年如一日 ...
- Java 里的异常(Exception)详解
作为一位初学者, 本屌也没有能力对异常谈得很深入. 只不过Java里关于Exception的东西实在是很多. 所以这篇文章很长就是了.. 一, 什么是java里的异常 由于java是c\c++ ...
- C++异常之七 标准库里的异常类
标准库里的异常类 C++标准提供了一组标准异常类,这些类以基类 Exception 开始,标准程序库抛出的所有异常,都派生于该基类,这些类构成如图所示的异常类的派生继承关系,该基类提供一个成员函数 w ...
随机推荐
- tensorflow训练打游戏ai
python3,所需模块请自行补齐 # coding=utf8 import pygame import random from pygame.locals import * import numpy ...
- Vue2.0 分页插件pagination使用详细说明
Vue2.0 分页pagination使用 插件下载地址:Vue_Pagination 插件描述:基于jQuery的分页插件大家都用过很多了吧,今天分享一下基于Vue的分页插件pagination.j ...
- magento的常用调用
1,CMS调用网站的Url <a href="{{store direct_url="about-us"}}">About Us</a> ...
- Hign-Speed Tracking with Kernelzied Correlation Filters
reference:Hign-Speed Tracking with Kernelzied Correlation Filters questions: The core componet of mo ...
- Springboot中使用缓存
在开发中,如果相同的查询条件去频繁查询数据库, 是不是会给数据库带来很大的压力呢?因此,我们需要对查询出来的数据进行缓存,这样客户端只需要从数据库查询一次数据,然后会放入缓存中,以后再次查询时可以从缓 ...
- HDU 1114:Piggy-Bank(完全背包)
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 《DSP using MATLAB》Problem 4.16
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 【正则表达式】java应用正则表达式
一:简单应用 /** * * ' * & * ' * & * & * ' * ' * ' * sources=sdcg'hde&xyz'dfa&&ad' ...
- IEPNGFix 解决IE6支持PNG透明问题
IE6本身是支持索引色透明度(PNG8)格式,但不支持真彩色透明度(PNG24)格式. 使用IE PNG Fix 2.0可以完美解决IE6支持PNG透明问题,而且支持背景定位和重复属性. IE PNG ...
- 注册表禁用和启用USB端口
USB端口禁用把下面代码另存为文件:USB_Disable.batcSCript \\AppServices\netlogon\USB_Disable.vbs--------------------- ...