简体   繁体   中英

Is queue object automatically shared among Processes from python multiprocessing module?

I have recently start working with Python multiprocessing module. I understand explanation of queues, but recently I found on https://pymotw.com/2/multiprocessing/communication.html that queues don't need to be pass as args to Proccess constructor method, eg

p = Process(target=f, args=(q,)),

instead, it seems that they are globally shared. I thought that this is only the case when we have managed queues, ie

queue = manager.Queue()

Can someone help me to understand this?

In Unix, a child process is created with fork() .

In Windows, a child process is created by invoking the same script with special arguments .

In both cases, there may be the q variable in the child process because it inherited the state or because the relevant code has run before execution reached the worker function.

But that is not enough. An IPC needs to be set up between the processes for it to play its role as a communication channel. Otherwise, it's just a regular local object.

When in doubt, see the official documentation which is the authoritative information source and is generally of exceptional quality. With multiprocessing , it's especially important to stick to the docs because due to its quirky nature, various things may seem to work but break in unpredictable ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM