Sharing list type data between processes by parallel processing using Python Multiprocessing

1. Introduction When implementing parallel processing using Multiprocessing in Python, I got stuck in the process of sharing array type data between processes, so I will leave it as a memorandum. The version of python when implemented is 3.7.1.

2. Implementation details The implementation content is the process of sharing the list taken as an argument by parallel processing the following two functions between processes. ① Process to display the list received as an argument every 3 seconds (2) Processing to add a to the list received as an argument every 2 seconds

I used a Manager object for state management between processes.

3. Wrong implementation ```python from multiprocessing import Process, Manager import time

#Show list every 3 seconds def list_print(test_list): while True: print(str(test_list)) time.sleep(3)

#Add a to the list every 2 seconds def list_append(test_list): while True: test_list.append("a") time.sleep(2)

if name == 'main': manager = Manager() test_list=manager.list() print("List before function execution" +str(test_list)) p1=Process(target=list_print,args=(test_list)) p2=Process(target=list_append,args=(test_list)) p1.start() p2.start() p1.join() p2.join()


 <h2> 4. Execution result </ h2>

Traceback (most recent call last): Traceback (most recent call last): File "/Applications/anaconda3/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap self.run() File "/Applications/anaconda3/lib/python3.7/multiprocessing/process.py", line 99, in run self._target(*self._args, **self._kwargs) File "/Applications/anaconda3/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap self.run() File "/Applications/anaconda3/lib/python3.7/multiprocessing/process.py", line 99, in run self._target(*self._args, **self._kwargs) TypeError: list_append() missing 1 required positional argument: 'test_list' TypeError: list_print() missing 1 required positional argument: 'test_list'


 Apparently, the argument was taken incorrectly and an error occurred.
 I wondered if the Manager object doesn't support list types, but it officially says it does support lists.
 So after reading the reference for a while, I found a solution.

 <h2> 5. Solution </ h2>
 I solved it by defining a dictionary as the first argument of the function to be executed.
 It defines an empty dictionary dummy and takes it as an argument.
 The code is below.


```python
from multiprocessing import Process,Manager
import time

#Show list
def list_print(dummy, test_list):
    while True:
        print(str(test_list))
        time.sleep(3)

#Add a to the list
def list_append(dummy, test_list):
    while True:
        test_list.append("a")
        time.sleep(2)

if __name__ == '__main__':
    manager = Manager()
    #Define an empty dictionary
    dummy = manager.dict()
    test_list=manager.list()
    print("List before function execution" +str(test_list))
   #Add an empty dictionary to the first argument
    p1=Process(target=list_print,args=(dummy, test_list))
    p2=Process(target=list_append,args=(dummy, test_list))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

6. Execution result The execution result is as follows. I was able to confirm the display and addition of the array safely.
List before function execution[]
[]
['a', 'a']
['a', 'a', 'a']
['a', 'a', 'a', 'a', 'a']

that's all.

Recommended Posts

Sharing list type data between processes by parallel processing using Python Multiprocessing
Python parallel processing (multiprocessing and Joblib)
[Python] Various data processing using Numpy arrays
How to take multiple arguments when doing parallel processing using multiprocessing in python
Process csv data with python (count processing using pandas)
Communication processing by Python
Data analysis using Python 0
Parallel processing with multiprocessing
Python data type summary memo
Image data type conversion [Python]
Data analysis using python pandas
Using Python mode in Processing
Japanese Natural Language Processing Using Python3 (4) Sentiment Analysis by Logistic Regression
Python parallel processing (multiprocessing and Joblib)
How to take multiple arguments when doing parallel processing using multiprocessing in python