tornado 拿到 gen.return 异步返回的结果后,没有在 yield 的地方恢复继续执行 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
tyles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
mactec

tornado 拿到 gen.return 异步返回的结果后,没有在 yield 的地方恢复继续执行

  •  
  •   mactec 2017 年 8 月 29 日 3345 次点击
    这是一个创建于 3161 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教大伙一个问题,我调试了半天,发现 gen.return 返回异步结果后,程序不是接着 yield 的地方执行,而是又跳到 ioloop,tornado 初学,文档较少,希望大家能帮忙指出错误在哪,感激 部分程序如下


    @gen.coroutine def _fetch_and_extract(self, task): self.processing_task_number += 1 self.processing_task_set.add(task) if self.processing_task_number != self.processing_task_set.size():ioloop.IOLoop.instance().stop() self.logger.info("%s: start to fetch and extract" % self.worker_name) if check_task(task): try: spider = load_object(task["spider"]) except Exception, e: handle_fail_task(task,"load %s object failed" % (task["spider"]),self.process_fail_task_queue, self.wait_for_process_task_queue) self.logger.error("%s: load object failed.path:%s, exception:%s" % (self.worker_name, task["spider"], e)) else: spider_object = spider(self.processed_url_set, self.wait_for_process_task_queue) fetch_start_time = datetime.datetime.now() resp = yield spider_object.fetch(task["request"]) self.logger.debug("got resp already ”)#已打印 #这里已经拿到异步返回的 resp 了,但是代码没有恢复接着往下走,而是直接回到 loop 去取 task 了,但是这时候 task 里空了,所以之后就一直提示任务空 #相当于只抓了美团 api 的 city 列表,后面 extract、再添加 task 都没有执行,我 debug 时候比较奇怪这点,按道理异步返回 resp 了应该接着之前代码的位置继续执行 不是么 if resp == None or resp.error != None: handle_fail_task(task, "fetch request: %s failed,code:%d error:%s" % (task["request"], resp.code, resp.error), self.process_fail_task_queue, self.wait_for_process_task_queue) self.logger.error("%s: fetch request: %s failed, error: %s" % (self.worker_name, task["request"], resp)) else: content_length = resp.headers['Content-Length'] if resp.headers.has_key('Content-Length') else None last_modified = resp.headers['Last-Modified'] if resp.headers.has_key("Last-Modified") else None fetch_time = datetime.datetime.now() - fetch_start_time extract_start_time = datetime.datetime.now() status = yield spider_object.extract(resp, **dict(task["kwargs"]) 
    3 条回复    2017-09-02 17:13:01 +08:00
    mactec
        1
    mactec  
    OP
       2017 年 8 月 29 日
    mark 下,如果有答案了再来更新
    bluesky139
        2
    bluesky139  
       2017 年 8 月 30 日
    肉眼表示没看出什么问题,你在那句打印出来 log 的地方以下每隔一行打 log 出来看看呢,或者能否精简个可独立运行的简陋版本出来
    mactec
        3
    mactec  
    OP
       2017 年 9 月 2 日
    @bluesky139
    '''
    class Worker(object):
    def __init__(self,str):
    self._name = str
    self._installed = False
    def install(self):
    if self._installed:
    print "%s: no need to start again.worker has been installed!" % self._name
    else:
    self._installed = True
    ioloop.IOLoop.instance().add_callback(self.test)
    print "%s: worker is installed" % self._name
    @gen.coroutine
    def fetch(self,url):
    req = HTTPRequest(url,connect_timeout=3,request_timeout=5)
    client = httpclient.AsyncHTTPClient()
    resp = yield gen.Task(client.fetch, req)
    raise gen.Return(resp)

    @gen.coroutine
    def exact(self,resp):
    yield gen.sleep(5)
    raise gen.Return(10)

    @gen.coroutine
    def test(self):
    if self._installed:
    ioloop.IOLoop.instance().add_timeout(datetime.timedelta(microsecOnds=5000),self.test)
    str = self._name
    task_url = u'http://api.caipiao.163.com/missNumber_trend.html?gameEn=kuai3'
    resp = yield self.fetch(task_url)
    print "%s get resp already at %s" %(str,datetime.datetime.now())
    staus = yield self.exact(resp)
    print "callback!!!%s status returned %d at %s" %(str,staus,datetime.datetime.ow())


    if __name__ == '__main__':
    arrs = ['aaa','bbb']
    for arr in arrs:
    worker = Worker(arr)
    worker.install()
    ioloop.IOLoop.instance().start()

    '''

    谢谢,还在找原因
    请问 ioloop.IOLoop.instance().add_timeout(datetime.timedelta(microsecOnds=5000),self.test)
    这个能让 test 方法定期执行么,测试结果这个间隔没有用
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2420 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 31ms UTC 01:02 PVG 09:02 LAX 18:02 JFK 21:02
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86