I used a Manager object for state management between processes.
#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()
 List before function execution[]
[]
['a', 'a']
['a', 'a', 'a']
['a', 'a', 'a', 'a', 'a']
that's all.
Recommended Posts