で働いています。あなたはおそらくキューと同じことをすることができますが、それらをもう少し管理する必要があります。 zeromqソケットはこのIMOのようなものにはうってつけです。
"""
demo of multiple processes doing processing and publishing the results
to a common subscriber
"""
from multiprocessing import Process
class Worker(Process):
def __init__(self, filename, bind):
self._filename = filename
self._bind = bind
super(Worker, self).__init__()
def run(self):
import zmq
import time
ctx = zmq.Context()
result_publisher = ctx.socket(zmq.PUB)
result_publisher.bind(self._bind)
time.sleep(1)
with open(self._filename) as my_input:
for l in my_input.readlines():
result_publisher.send(l)
if __name__ == '__main__':
import sys
import os
import zmq
#assume every argument but the first is a file to be processed
files = sys.argv[1:]
# create a worker for each file to be processed if it exists pass
# in a bind argument instructing the socket to communicate via ipc
workers = [Worker(f, "ipc://%s_%s" % (f, i)) for i, f \
in enumerate((x for x in files if os.path.exists(x)))]
# create subscriber socket
ctx = zmq.Context()
result_subscriber = ctx.socket(zmq.SUB)
result_subscriber.setsockopt(zmq.SUBSCRIBE, "")
# wire up subscriber to whatever the worker is bound to
for w in workers:
print w._bind
result_subscriber.connect(w._bind)
# start workers
for w in workers:
print "starting workers..."
w.start()
result = []
# read from the subscriber and add it to the result list as long
# as at least one worker is alive
while [w for w in workers if w.is_alive()]:
result.append(result_subscriber.recv())
else:
# output the result
print result
ああとzmqがちょうど
$ pip install pyzmq-static
これがすべてでうまく仕事に行くのではありません取得します。 1つのリストに対するすべての競合で、おそらく並行性からの利益の多くを見ないでしょう。 –
@Rafe Kettler:何をお勧めしますか?ここでの私の目標は、処理の最後に結果を「集約」できることです。代替案は何ですか? (物事を明確にするために、出力リストのサイズは〜100 [MB]になります)。 – user3262424
私はzeromqで動作しているのを見ることができます。複数の出版社、1人の加入者。加入者がリストを管理する。 私が持っている質問は#1です:テーブルの3番目のライブラリですか? #2:出力はどのように表現されますか? –