从多个进程向队列添加数据

时间:2019-02-24 05:01:27

标签: python python-3.x

我试图多次调用相同的流程并获取回报

from time import sleep
from multiprocessing import Process, Queue
from multiprocessing import Pool

def main():
    #Call processes here.
    var1 = 100000000
    var2 = 1000000
    var3 = 100000

    q = Queue()

    p1 = Process(target=call_pool, args=(q,var1))
    p2 = Process(target=call_pool, args=(q,var2))
    p3 = Process(target=call_pool, args=(q,var3))

    p1.start()
    p2.start()
    p3.start()

    p1.join()
    p2.join()
    p3.join()

    print q.get()    # prints "[42, None, 'hello']"

def call_pool(queue,var):
    #Call pool here
    queue.put(len(pool_gen(var)))


def pool_gen(var):
    pool = Pool()
    data = pool.map(f, range(var))
    return data

def f(x):
    return x*x*x*x

if __name__ == '__main__':
    start = time.time()
    main()
    end = time.time()
    print(end - start)

我认为队列没有添加函数。 我是否需要定义3个队列并将数据放入3倍以上?

1 个答案:

答案 0 :(得分:0)

我猜答案是使用经理...

import time
from time import sleep
from multiprocessing import Process, Manager
from multiprocessing import Pool

def main():
    #Call processes here.
    var1 = 100000
    var2 = 100000
    var3 = 100000

    manager = Manager()
    d = manager.list()

    p1 = Process(target=call_pool, args=(d,var1))
    p2 = Process(target=call_pool, args=(d,var2))
    p3 = Process(target=call_pool, args=(d,var3))

    p1.start()
    p2.start()
    p3.start()

    p1.join()
    p2.join()
    p3.join()

    print d    # prints "[42, None, 'hello']"

def call_pool(d,var):
    #Call pool here
    d.append(len(pool_gen(var)))


def pool_gen(var):
    pool = Pool()
    data = pool.map(f, range(var))
    return data

def f(x):
    return x*x*x*x

if __name__ == '__main__':
    start = time.time()
    main()
    end = time.time()
    print(end - start)