0
共有されていないに:私が得たものPythonのマルチプロセッシングキューが、私は以下のコードを実行しようとしたメモリ
import multiprocessing
import time
def init_queue():
print("init g_queue start")
while not g_queue.empty():
g_queue.get()
for _index in range(10):
g_queue.put(_index)
print("init g_queue end")
return
def task_io(task_id):
print("IOTask[%s] start" % task_id)
print("the size of queue is %s" % g_queue.qsize())
while not g_queue.empty():
time.sleep(1)
try:
data = g_queue.get(block=True, timeout=1)
print("IOTask[%s] get data: %s" % (task_id, data))
except Exception as excep:
print("IOTask[%s] error: %s" % (task_id, str(excep)))
print("IOTask[%s] end" % task_id)
return
g_queue = multiprocessing.Queue()
if __name__ == '__main__':
print("the size of queue is %s" % g_queue.qsize())
init_queue()
print("the size of queue is %s" % g_queue.qsize())
time_0 = time.time()
process_list = [multiprocessing.Process(target=task_io, args=(i,)) for i in range(multiprocessing.cpu_count())]
for p in process_list:
p.start()
for p in process_list:
if p.is_alive():
p.join()
print("End:", time.time() - time_0, "\n")
は以下の通りであった:
the size of queue is 0
init g_queue start
init g_queue end
the size of queue is 10
IOTask[0] start
the size of queue is 0
IOTask[0] end
IOTask[1] start
the size of queue is 0
IOTask[1] end
('End:', 0.6480000019073486, '\n')
だった私は何を期待していたが
IOTask[0] start
the size of queue is 10
g_queueの初期化後、キューのサイズは0ではなく、10であるはずでした。キューが存在しないようです共有メモリサブプロセスが起動すると、g_queueのコピーが作成され、サイズは0になります。
なぜマルチプロセッシングキューは共有メモリにありませんか?お知らせ下さい。どうもありがとう!
とマルチプロセッシング使用するための
デモは、はい、それは仕事をしません。どうもありがとう。 – Ryan