Python 2.7多处理在不使用池的情况下获取处理结果

标签 python process multiprocessing

如何在不使用池的情况下从进程中获取结果?

(我愿意密切关注进展:

(print "\r",float(done)/total,"%",)

据我所知,这是无法使用池来完成的)

def multiprocess(function, argslist, ncpu):
    total = len(argslist)
    done = 0
    jobs = []
    while argslist != []:
        if len(mp.active_children()) < ncpu:
            p = mp.Process(target=function,args=(argslist.pop(),))
            jobs.append(p)
            p.start()
            done+=1
            print "\r",float(done)/total,"%",
    #get results here
    for job in jobs:
        job.get_my_result()???

这些进程非常短(<0.5 秒),但我有大约 100 万个进程。

我看到了这个帖子Can I get a return value from multiprocessing.Process?我尝试重现它,但无法使其正常工作。

您可以随时获取任何进一步的信息。

最佳答案

这个问题可能被认为是重复的,但无论如何,这是我的问题的解决方案:

def multiprocess(function, argslist, ncpu):
    total = len(argslist)
    done = 0
    result_queue = mp.Queue()
    jobs = []
    while argslist != [] and done<10 :
        if len(mp.active_children()) < ncpu:
            p = mp.Process(target=function,args=(result_queue, argslist.pop(),))
            jobs.append(p)
            p.start()
            done+=1
            print "\r",float(done)/total,"%",
    #get results here
    res = [result_queue.get() for p in jobs]
    print res

我也必须改变

return function_result

进入

result_queue.put(function_result)

关于Python 2.7多处理在不使用池的情况下获取处理结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30189270/

相关文章:

python - 在 Tornado 应用程序中使用 Django - 无法访问 Tornado 应用程序启动后创建的 MySQL 记录

python - 如何在Groupby中保留其他列的行值?

c# - Waitforexit 统一中断我正在运行的应用程序

python - 煎饼排序中最短翻转序列的计数

Python 同时填充列表

python - Numpy:创建 3D 数组的矢量化操作

python - 我如何检查理解的时间复杂度

python - 为什么这种Python多线程方法比单线程方法需要更多的时间来解决相同的任务?

windows - 通过从 .BAT 中查找进程正在使用的端口来终止进程

python - 向通过 pool.map 调用的函数添加状态——如何避免酸洗错误