美文网首页
python 执行程序 被killed

python 执行程序 被killed

作者: 王镇_ee87 | 来源:发表于2023-09-15 18:35 被阅读0次

遇到的问题,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)


相关文章

网友评论

      本文标题:python 执行程序 被killed

      本文链接:https://www.haomeiwen.com/subject/ogfbvdtx.html