简体   繁体   English

使用多处理从其他流程获取数据

[英]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: 这是我的两个问题:

  1. 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? 我应该使用ValueManager还是其他?

  2. 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.

相关问题 使用python多处理从进程获取结果 - Obtain results from processes using python multiprocessing 使用多处理并尝试使用队列中的数据,并在数据进入多处理队列后立即打印该数据的结果 - Using multiprocessing and trying to use data in queue and print result from that data as soon as data get in multiprocessing queue 在 Python 的多处理模块中打印来自并行进程的数据 - Print data from parallel processes in Python's multiprocessing module 使用多处理从不同进程附加到同一列表 - Appending to the same list from different processes using multiprocessing 使用多处理从多个进程中排队tf.RandomShuffleQueue - Enqueuing a tf.RandomShuffleQueue from multiple processes using multiprocessing 如何使用多重处理从多个函数获取返回的数据 - How to get returned data from multiple functions using multiprocessing 使用多处理嵌套并行进程 - Nesting parallel processes using multiprocessing Python多重处理可防止关闭其他进程 - Python multiprocessing prevent switching off to other processes 使用多处理模块运行并行进程,其中一个进程由另一个进程供(依赖)维特比算法 - Using multiprocessing module to runs parallel processes where one is fed (dependent) by the other for Viterbi Algorithm 无法进行多处理以同时运行进程 - Can't get multiprocessing to run processes concurrently
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM