[英]Get Data from Other Processes using Multiprocessing
(Language is Python 3) (语言是Python 3)
I am writing a program with the module multiprocessing
and using Pool
. 我正在使用模块multiprocessing
并使用Pool
编写程序。 I need some variable that is shared between all of the processes. 我需要在所有进程之间共享的一些变量。 The parent process will initialize this variable and pass it as an argument to p.map()
. 父进程将初始化此变量,并将其作为参数传递给p.map()
。 I want the child processes to change this variable. 我希望子进程更改此变量。 The intent of this is because the first part of the child processes' work should be done in parallel (computational work that doesn't need any other processes' data). 这样做的目的是因为子流程工作的第一部分应该并行完成(不需要任何其他流程数据的计算工作)。 But, the second part of the processes' work needs to be done in order, one process after another, because they are writing to a file and the contents of that file should be in order. 但是,流程工作的第二部分需要按顺序完成,一个接一个地处理,因为它们正在写入文件,并且该文件的内容应该井井有条。 I want each process to wait until the others are done before moving on. 我希望每个过程都可以等到其他过程完成后再继续。 I will record the "progress" of the entire program with the variable, eg when the first process is done writing to the file, it will increment the variable by one. 我将用变量记录整个程序的“进度”,例如,当第一个过程完成向文件的写入时,它将使变量增加一个。 I want this to be a signal to the next process in line to begin writing to the file. 我希望这是开始写入文件的下一个过程的信号。 But I need some sort of waituntil()
function to make the processes wait until the Value
variable indicates that it is their "turn" to write to the file. 但是我需要某种waituntil()
函数来使进程等待,直到Value
变量指示它是写入文件的“转身”为止。
Here are my two problems: 这是我的两个问题:
I need a variable that the child processes can edit, and the child processes can actually get the value of that variable. 我需要一个子进程可以编辑的变量,并且子进程实际上可以获取该变量的值。 What type of variable should I use? 我应该使用哪种类型的变量? Should I use Value
, Manager
, or something else? 我应该使用Value
, Manager
还是其他?
I need the processes to wait until the variable described above equals to a certain value, signaling that it is their turn to write to the file. 我需要过程等待,直到上述变量等于某个值,这表明该轮到他们写文件了。 Is there any sort of waituntil()
function that I can use? 我可以使用任何一种waituntil()
函数吗?
What you are looking for is called Synchronization . 您正在寻找的被称为同步 。 There are multitudes of different synchronization primitives to choose from. 有许多不同的同步原语可供选择。
You should never attempt to write synchronization primitives on your own, as it is non-trivial to do correctly! 您永远不要尝试自己编写同步原语,因为正确地做到这一点并不容易!
In your case either an Event or a Condition might be suitable. 在您的情况下, 事件或条件可能都合适。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.