这里的job.get()是什么含义?

作者站长头像
站长
· 阅读数 12

代码来源https://medium.com/geekculture/python-multiprocessing-with-ou...

import multiprocessing as mp

def worker_function(item):
    """
    do some work, write results to output
    """
    res = f'item: {item} - result: {item ** 2}'
    print(res)
    with open('output_no_queue.txt', 'a') as f:
        f.write(str(res) + '\n')


if __name__ == '__main__':
    pool = mp.Pool(16)
    jobs = []
    for item in range(10000):
        job = pool.apply_async(worker_function, (item, ))
        jobs.append(job)

    for job in jobs:
        job.get()

    pool.close()
    pool.join()

这里的job.get()是表达什么呢?jobs是个list,每个job也不是queue,list的元素没有get方法,如何理解呢?

回复
1个回答
avatar
test
2024-07-02

answer image看看这个图就是知道了,用来解决并发的

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容