如何包装异步发电机的功能一起龙卷风? [英] How to wrap asynchronous and gen functions together in Tornado?

查看:175
本文介绍了如何包装异步发电机的功能一起龙卷风?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何包装异步发电机的功能一起龙卷风?
我的code看起来像下面,错误的是'未来'对象有没有属性'身体'。

难道我把装饰以错误的方式?

 进口tornado.httpclient
进口tornado.web
进口tornado.gen
进口tornado.httpserver
进口tornado.ioloop类class1(tornado.web.RequestHandler):    @ tornado.web.asynchronous
    高清职位(自我,* ARGS,** kwargs):
        URL = self.get_argument('URL',无)
        响应= self.json_fetch('POST',网址,self.request.body)
        self.write(response.body)
        self.finish()    @ tornado.gen.engine
    高清json_fetch(自我,方法,URL,体=无,* ARGS,** kwargs):
        客户端= tornado.httpclient.AsyncHTTPClient()
        标题= tornado.httputil.HTTPHeaders({内容类型:应用/ JSON的charset = UTF-8})
        请求= tornado.httpclient.HTT prequest(URL,方法,头,体)
        产量tornado.gen.Task(client.fetch,请求)


解决方案

您不必在此code比如异步。 gen.engine已经过时了,用协程代替。你不一般需要使用gen.Task很多这些天来,无论是。让四个转变到code:


  1. 裹在协程,后

  2. 产量self.json_fetch而不是直接使用结果的结果。

  3. 无需在协程调用完成,结束旋风响应协同程序完成时。

  4. 裹在json_fetch协程了。

结果:

 类ClubCreateActivity(tornado.web.RequestHandler):    @ tornado.gen.coroutine
    高清职位(自我,* ARGS,** kwargs):
        URL = self.get_argument('URL',无)
        响应=产量self.json_fetch('POST',网址,self.request.body)
        self.write(response.body)    @ tornado.gen.coroutine
    高清json_fetch(自我,方法,URL,体=无,* ARGS,** kwargs):
        客户端= tornado.httpclient.AsyncHTTPClient()
        标题= tornado.httputil.HTTPHeaders({内容类型:应用/ JSON的charset = UTF-8})
        请求= tornado.httpclient.HTT prequest(URL,方法,头,体)
        响应=产量client.fetch(要求)
        提高gen.Return(响应)

延伸阅读:

How to wrap asynchronous and gen functions together in Tornado? My code looks like below, the error is 'Future' object has no attribute 'body'.

Did I place the decorators in a wrong way?

import tornado.httpclient
import tornado.web
import tornado.gen
import tornado.httpserver
import tornado.ioloop

class Class1(tornado.web.RequestHandler):

    @tornado.web.asynchronous
    def post(self, *args, **kwargs):
        url = self.get_argument('url', None)
        response = self.json_fetch('POST', url, self.request.body)
        self.write(response.body)
        self.finish()

    @tornado.gen.engine
    def json_fetch(self, method, url, body=None, *args, **kwargs):
        client = tornado.httpclient.AsyncHTTPClient()
        headers = tornado.httputil.HTTPHeaders({"content-type": "application/json charset=utf-8"})
        request = tornado.httpclient.HTTPRequest(url, method, headers, body)
        yield tornado.gen.Task(client.fetch, request)

解决方案

You don't need "asynchronous" in this code example. "gen.engine" is obsolete, use "coroutine" instead. You don't generally need to use "gen.Task" much these days, either. Make four changes to your code:

  1. Wrap "post" in "coroutine"
  2. "yield" the result of self.json_fetch instead of using the result directly.
  3. No need to call "finish" in a coroutine, Tornado finishes the response when a coroutine completes.
  4. Wrap json_fetch in "coroutine", too.

The result:

class ClubCreateActivity(tornado.web.RequestHandler):

    @tornado.gen.coroutine
    def post(self, *args, **kwargs):
        url = self.get_argument('url', None)
        response = yield self.json_fetch('POST', url, self.request.body)
        self.write(response.body)

    @tornado.gen.coroutine
    def json_fetch(self, method, url, body=None, *args, **kwargs):
        client = tornado.httpclient.AsyncHTTPClient()
        headers = tornado.httputil.HTTPHeaders({"content-type": "application/json charset=utf-8"})
        request = tornado.httpclient.HTTPRequest(url, method, headers, body)
        response = yield client.fetch(request)
        raise gen.Return(response)

Further reading:

这篇关于如何包装异步发电机的功能一起龙卷风?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆