While I was using multiprocessing, I found out that global variables are not shared between processes.
Example of the Issue
Let me first provide an example of the issue that I was facing.
I have 2 input lists, which 2 processes wil read from and append them to the final list and print the aggregated list to stdout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
When running the example:
1 2 |
|
As you can see the response from the list is still empty.
Resolution
We need to use multiprocessing.Manager.List.
From Python’s Documentation:
“The multiprocessing.Manager
returns a started SyncManager object which can be used for sharing objects between processes. The returned manager object corresponds to a spawned child process and has methods which will create shared objects and return corresponding proxies.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Now when we run our script, we can see that our processes are aware of our defined list:
1 2 |
|
Thank You
Please feel free to show support by, sharing this post, making a donation, subscribing or reach out to me if you want me to demo and write up on any specific tech topic.