gpt4 book ai didi

python - 如何在 Python 中使用多处理队列?

转载 作者:IT老高 更新时间:2023-10-28 21:41:49 29 4
gpt4 key购买 nike

我很难理解多处理队列在 python 上的工作原理以及如何实现它。假设我有两个从共享文件访问数据的 python 模块,我们将这两个模块称为写入器和读取器。我的计划是让读取器和写入器都将请求放入两个单独的多处理队列中,然后让第三个进程在循环中弹出这些请求并执行。

我的主要问题是我真的不知道如何正确实现 multiprocessing.queue,你不能真正为每个进程实例化对象,因为它们将是单独的队列,你如何确保所有进程都与共享队列(或者在这种情况下,队列)

最佳答案

My main problem is that I really don't know how to implement multiprocessing.queue correctly, you cannot really instantiate the object for each process since they will be separate queues, how do you make sure that all processes relate to a shared queue (or in this case, queues)

这是一个读取器和写入器共享单个队列的简单示例……写入器向读取器发送一堆整数;当 writer 用完数字时,它会发送“DONE”,让 reader 知道要跳出读取循环。

您可以根据需要生成任意数量的读取器进程...

from multiprocessing import Process, Queue
import time
import sys


def reader_proc(queue):
"""Read from the queue; this spawns as a separate Process"""
while True:
msg = queue.get() # Read from the queue and do nothing
if msg == "DONE":
break


def writer(count, num_of_reader_procs, queue):
"""Write integers into the queue. A reader_proc() will read them from the queue"""
for ii in range(0, count):
queue.put(ii) # Put 'count' numbers into queue

### Tell all readers to stop...
for ii in range(0, num_of_reader_procs):
queue.put("DONE")


def start_reader_procs(qq, num_of_reader_procs):
"""Start the reader processes and return all in a list to the caller"""
all_reader_procs = list()
for ii in range(0, num_of_reader_procs):
### reader_p() reads from qq as a separate process...
### you can spawn as many reader_p() as you like
### however, there is usually a point of diminishing returns
reader_p = Process(target=reader_proc, args=((qq),))
reader_p.daemon = True
reader_p.start() # Launch reader_p() as another proc

all_reader_procs.append(reader_p)

return all_reader_procs


if __name__ == "__main__":
num_of_reader_procs = 2
qq = Queue() # writer() writes to qq from _this_ process
for count in [10**4, 10**5, 10**6]:
assert 0 < num_of_reader_procs < 4
all_reader_procs = start_reader_procs(qq, num_of_reader_procs)

writer(count, len(all_reader_procs), qq) # Queue stuff to all reader_p()
print("All reader processes are pulling numbers from the queue...")

_start = time.time()
for idx, a_reader_proc in enumerate(all_reader_procs):
print(" Waiting for reader_p.join() index %s" % idx)
a_reader_proc.join() # Wait for a_reader_proc() to finish

print(" reader_p() idx:%s is done" % idx)

print(
"Sending {0} integers through Queue() took {1} seconds".format(
count, (time.time() - _start)
)
)
print("")

关于python - 如何在 Python 中使用多处理队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11515944/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com