遇到的问题,python程序运行中被killed。
代码
def func1(k):
time.sleep(1)
print(k)
# return 'aaa'
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
if __name__ == '__main__':
i = [1, 2, 3, 4, 5]
while 1:
executor = ThreadPoolExecutor(5) # 创建一个最大容量为16的线程池
rrr = executor.map(func1, i) # 通过线程池的map方法,将任务提交给线程池, map返回的是线程执行的结果的生成器对象
print('0000000000')
结果
0000000000
1
5
3
4
2
0000000000
3
1
4
5
2
原因 开了多线程 和while 循环 没有等结束就开下一轮 导致内存泄漏
解决
def func1(k):
time.sleep(1)
print(k)
# return 'aaa'
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
if __name__ == '__main__':
i = [1, 2, 3, 4, 5]
while 1:
executor = ThreadPoolExecutor(5) # 创建一个最大容量为16的线程池
# rrr = executor.map(func1, i) # 通过线程池的map方法,将任务提交给线程池, map返回的是线程执行的结果的生成器对象
[j for j in executor.map(func1,i )]
print('0000000000')
time.sleep(6)