tornado 비동기 클라이언트(gen.coroutine)

1340 단어
먼저 실행 가능한 코드를 열거하고, 주석과 설명을 후속으로 보충한다.
# -.- coding:utf-8 -.-
# __author__ = 'zt'
from tornado import gen
import tornado.ioloop
from tornado.httpclient import AsyncHTTPClient


@gen.coroutine
def fetch_coroutine(url):
    http_client = AsyncHTTPClient()
    response = yield http_client.fetch(url)
    raise gen.Return(response.body)


def main():
    return fetch_coroutine('http://www.qq.com')

if __name__ == '__main__':
    response = tornado.ioloop.IOLoop.instance().run_sync(main)
    print response
# -.- coding:utf-8 -.-
# __author__ = 'zt'
import tornado.ioloop
from tornado.httpclient import AsyncHTTPClient
from tornado import gen


def futrue_callback(res_future):
    print('zzz', res_future.result().decode('gbk'))


@gen.coroutine
def fetch_coroutine(url):
    http_client = AsyncHTTPClient()
    response = yield http_client.fetch(url)
    raise gen.Return(response.body)


if __name__ == '__main__':
    s = fetch_coroutine('http://www.qq.com')
    io_loop = tornado.ioloop.IOLoop.current()
    io_loop.add_future(s, futrue_callback)
    io_loop.start()

참조:
참조:

좋은 웹페이지 즐겨찾기