【发布时间】:2019-04-09 03:16:31
【问题描述】:
我想创建一个包含大约 256K 文件路径的队列,并让这些路径出列并由并行工作进程处理。这是多处理而不是线程。
但是,当我创建一个 multiprocessing.queue 时,队列中似乎有 32K 个对象的硬限制。如果对象是文件的完整路径,这可能会更小。
为多处理创建多服务器队列的另一种方法是什么?
import multiprocessing
import sys
q = multiprocessing.Queue()
for i in range(32768 * 2):
print i
try:
q.put('abcdef')
except:
print "Unexpected error on ()".format(i), sys.exc_info()[0]
raise
产量:
...
32766
32767
Traceback (most recent call last):
Unexpected error on () <type 'exceptions.KeyboardInterrupt'>
File "/Users/Wes/Dropbox/Programming/ElectionTransparency/vops_addons/dead/tryq.py", line 13, in <module>
q.put('abc')
File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/queues.py", line 101, in put
if not self._sem.acquire(block, timeout):
KeyboardInterrupt
【问题讨论】:
-
这种队列的想法通常是同时放入和取出项目。如果您先启动工作程序而不是等待完全填充队列,您就不会遇到这个问题。
-
或许可以考虑使用带有
LPUSH和BLPOP的Redis 列表,然后您可以从网络中的任何机器插入、删除和处理项目。