Writing to a file with multiprocessing

前端 未结 3 891
孤街浪徒
孤街浪徒 2020-12-09 04:34

I\'m having the following problem in python.

I need to do some calculations in parallel whose results I need to be written sequentially in a file. So I created a fu

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-09 05:03

    You really should use two queues and three separate kinds of processing.

    1. Put stuff into Queue #1.

    2. Get stuff out of Queue #1 and do calculations, putting stuff in Queue #2. You can have many of these, since they get from one queue and put into another queue safely.

    3. Get stuff out of Queue #2 and write it to a file. You must have exactly 1 of these and no more. It "owns" the file, guarantees atomic access, and absolutely assures that the file is written cleanly and consistently.

提交回复
热议问题