threadlocal与线程相关,每个线程都会有一份,参考 http://python.jobbole.com/86150/
ThreadPoolExecutor构造函数里面有max_workers参数,如果这个参数设置的不好,就有可能造成内存泄漏。
示例代码如下:
from concurrent.futures import ThreadPoolExecutor
import threading
import time
import traceback
x = threading.local()
def show():
try:
print(f'{threading.get_ident()} size {len(x.content)}')
except:
print(traceback.format_exc())
def func():
print(f'{threading.current_thread()}')
with open(r'/path/to/bigfile', 'rb') as f:
x.content = f.read()
print(len(x.content))
show()
time.sleep(15)
return '1'
if __name__ == '__main__':
executer = ThreadPoolExecutor(max_workers=4)
for i in range(4):
f1 = executer.submit(func)