背景
上次类似的文章,只是展示了将阻塞函数变为了非阻塞。 今天又有一些领悟。 @run_on_executor 装饰器的作用, 不管是变为非阻塞还可以 像celery 一个执行异步任务。
1、 将阻塞函数转非阻塞 函数 并获取到值
from tornado.concurrent import run_on_executor
import time
import tornado.web
import tornado.gen
from concurrent.futures import ThreadPoolExecutor
from concurrent import futures
import asyncio
class LogicAsyTest():
# executor = ThreadPoolExecutor(1)
# executor = ThreadPoolExecutor(255)
# executor = ThreadPoolExecutor(200)
# executor = ThreadPoolExecutor(100)
executor = ThreadPoolExecutor()
def __init__(self):
pass
@tornado.gen.coroutine
def bb(self):
res = yield self.my_sleep()
return res
async def execute(self, base_url):
""" 这个方法是非常好的的"""
result = Result.