系统规范-MacOS 10.13.6-Python 3.7.0-龙卷风5.1.1
我想使用ThreadPoolExecutor来运行阻塞函数在提供RESTful服务的Tornado实例中。
ThreadPool按预期工作,并并行生成四个工作线程(请参阅下面的代码和控制台日志),只要我不试图产生执行函数返回的结果。
ThreadPoolExecutor不产生结果
import time
import tornado.web
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor
from tornado.ioloop import IOLoop
MAX_WORKERS = 4
i = 0
class Handler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
@run_on_executor
def background_task(self, i):
print("going sleep %s" % (i))
time.sleep(10);
print("waking up from sleep %s" % (i))
return str(i)
@tornado.gen.coroutine
def get(self):
global i
i+=1
self.background_task(i)
def make_app():
return tornado.web.Application([
(r"/", Handler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8000, '0.0.0.0')
IOLoop.current().start()
控制台输出
going sleep 1
going sleep 2
going sleep 3
going sleep 4
waking up from sleep 1
going sleep 5
waking up from sleep 2
going sleep 6
waking up from sleep 3
going sleep 7
waking up from sleep 4
going sleep 8
waking up from sleep 5
going sleep 9
waking up from sleep 6
waking up from sleep 7
waking up from sleep 8
waking up from sleep 9
可以看出,有四个并行工作线程正在运行,一旦其中一个线程完成,就会执行一个排队的函数。
然而,当我尝试使用协程生成返回函数时,我会遇到一些问题。虽然它没有完全阻塞IoLoop,但它会在没有明确模式的情况下被延迟。
更改的代码:ThreadPoolExecutor现在产生结果
MAX_WORKERS = 4
i = 0
class Handler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
@run_on_executor
def background_task(self, i):
print("%s: Going sleep %s" % (time.time(), i))
time.sleep(10);
print("%s: Waiking up from sleep %s" % (time.time(), i))
return str(i)
@tornado.gen.coroutine
def get(self):
global i
i+=1
result = yield self.background_task(i)
self.write(result)
从控制台输出来看,只有一个线程为第一个和第二个请求运行,一旦单个线程完成,就会执行排队任务(导致启动任务2延迟10秒,启动任务3延迟10秒)。然而,任务3、4、5和6是并行执行的,但每次调用之间的延迟不同。
控制台输出
1548687401.331075: Going sleep 1
1548687411.333173: Waking up from sleep 1
1548687411.340162: Going sleep 2
1548687421.3419871: Waking up from sleep 2
1548687421.347039: Going sleep 3
1548687423.4030259: Going sleep 4
1548687423.884313: Going sleep 5
1548687424.6828501: Going sleep 6
1548687431.351986: Waking up from sleep 3
1548687431.3525162: Going sleep 7
1548687433.407232: Waking up from sleep 4
1548687433.407604: Going sleep 8
1548687433.8846452: Waking up from sleep 5
1548687433.885139: Going sleep 9
1548687434.685195: Waking up from sleep 6
1548687434.685662: Going sleep 10
1548687441.3577092: Waking up from sleep 7
1548687441.358009: Going sleep 11
1548687443.412503: Waking up from sleep 8
1548687443.888705: Waking up from sleep 9
1548687444.691127: Waking up from sleep 10
1548687451.359714: Waking up from sleep 11
有人能解释这种行为吗?你有什么办法吗?
当前Python Tornado在使用ThreadPoolExecutor时内存泄漏,因此Tornado不可用。
根据CPU和工作负载的配置,进程对任务进行拆分。我们可以使用简单的startchrome选项来验证并发性。