0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

キューを使ったマルチプロセス処理

Posted at

初めに

業務でmultiprocessingのQueueを使用して並列処理を実装することがあったのでノウハウ記録用に記入にします。誰かのお役に立てれば幸いです。

使用例

マルチプロセス処理の例を紹介します。

import multiprocessing as mp

def work(q):
    q.put(object()) #アイテムをキューに追加 最大処理数のリソースが消費されるまでブロック
    # do some work
    q.get() #アイテムを回収 リソースを解放 アイテムが取り出せるまでブロック

if __name__ == '__main__':
    Max = 2 #並列最大処理数
    queue = mp.Queue(Max)
    process = [mp.Process(target=work, args=(queue,)),
               mp.Process(target=work, args=(queue,)), ]
    [p.start() for p in process]
    [p.join() for p in process]


参考資料

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?