使用tornado.ioloop.IOLoop.run_in_executor时如何避免线程过多?

时间:2019-08-29 09:25:15

标签: python-3.x multithreading tornado python-asyncio concurrent.futures

我正在使用tornado.ioloop.IOLoop.run_in_executor将同步函数更改为异步,但是事实证明,每次调用该函数时,都会创建一个线程,但该线程不会被杀死。

这是一个最小的可重现示例(至少在我的机器上可重现):

#!/usr/bin/env python3                                                          

import time
import tornado.ioloop
import tornado.web

def slow_func():
    time.sleep(1)
    print('slow func returned')
    return 'succ\n'

class TestHandler(tornado.web.RequestHandler):
    async def get(self):
        print('GET called')
        try:
            result = await tornado.ioloop.IOLoop.current().run_in_executor(None, slow_func)
        except Exception as e:
            print(e)
        self.write(result)
        print('GET returned')

if __name__ == '__main__':
    tornado.web.Application([
        (r'/', TestHandler),
    ]).listen(3000)
    print('Serving at 3000')
    tornado.ioloop.IOLoop.current().start()

对此TestHandler的每个请求都将创建一个新线程来运行slow_func,但是该函数返回后该线程仍然存在。我可以在ps H中看到它们,它会创建新线程,直到达到ulimit。我的环境是:

$ uname -a
Linux xxx 2.6.32-754.6.3.el6.x86_64 #1 SMP Tue Sep 18 10:29:08 EDT 2018 x86_64 x86_64 x86_64 GNU/Linux
$ python3 --version
Python 3.7.4
$ pip3 show tornado
Name: tornado
Version: 6.0.3
Summary: Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.
Home-page: http://www.tornadoweb.org/
Author: Facebook
Author-email: python-tornado@googlegroups.com
License: http://www.apache.org/licenses/LICENSE-2.0
Location: /xxx/lib/python3.7/site-packages
Requires: 
Required-by:

tornado.ioloop.IOLoop.run_in_executor使用concurrent.futures.Executor并返回一个等待的Future对象。 [1] [2] [3]

函数返回后线程在做什么?为什么在将来的物体解析后他们不被杀死?我应该如何避免这种情况?

1 个答案:

答案 0 :(得分:1)

run_in_executorconcurrent.futures.Executor对象作为第一个参数。

您可以创建执行程序并限制线程池的大小:

from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(max_workers=8)

IOLoop.current().run_in_executor(executor, slow_func)