Multiprocessing in Python 3 get different values when running on Mac and Linux.
For example, when forking the variable values in the main process should be cloned to the child process. But after testing, Linux's result is different from Mac.
Here is the code
#!/usr/bin/env python
# coding=utf-8
import multiprocessing
test_temp = "a"
def check_value_in_other_process():
print(f"[{multiprocessing.current_process().name}] test_temp={test_temp}")
def change_value():
global test_temp
test_temp = "b"
if __name__ == '__main__':
print(f"[{multiprocessing.current_process().name}] origin test_temp={test_temp}")
change_value()
print(f"[{multiprocessing.current_process().name}] changed test_temp={test_temp}")
pool = multiprocessing.Pool(4)
pool.apply_async(func=check_value_in_other_process)
pool.close()
pool.join()
On Linux,I tested with Debian 9 and CentOS 8, the result is
[MainProcess] origin test_temp=a
[MainProcess] changed test_temp=b
[ForkPoolWorker-1] test_temp=b
On mac, I tested with mac os 14 and mac os 15, the result is:
[MainProcess] origin test_temp=a
[MainProcess] changed test_temp=b
[SpawnPoolWorker-2] test_temp=a
Maybe,the difference is caused by ForkPoolWorker and SpawnPoolWorker?
After check,I found that there is 3 ways to create process:spawn、fork、forkserver. So I add:
multiprocessing.set_start_method('fork')
Linux and Mac got the same result But:
- Why does mac act the different default behavior?
- How can I set the default behavior in my project?