Python implements multi-process data sharing, and python implements process Sharing
This example describes how to implement multi-process data sharing in Python. We will share this with you for your reference. The details are as follows:
Example 1:
#-*-Coding: UTF-8-*-from multiprocessing import Process, Managerimport timeimport randomdef kkk (a_list, number): for I in range (10): a_list.append (I) time. sleep (random. randrange (2) print ('this is the process {}{}'. format (number, a_list) print ('this is the process {}{}'. format (number, a_list) def jjj (a_list): for I in range (2): process = Process (target = kkk, args = (a_list, I) process. start () if _ name _ = '_ main _': a_list = [] process_0 = Process (target = jjj, args = (a_list ,)) process_0.start () process_0.join () print (a_list) print (len (a_list) print ('It \'s OK ')
Output:
This is process 0 [0] This is process 0 [0, 1] This is process 0 [0, 1, 2] This is process 0 [0, 1, 2, 3] This is process 1 [0] This is process 0 [0, 1, 2, 3, 4] This is process 1 [0, 1] This is process 0 [0, 1, 2, 3, 4, 5] This is process 1 [0, 1, 2] This is process 1 [0, 1, 2, 3] This is process 1 [0, 1, 2, 3, 4] This is process 1 [0, 1, 2, 3, 4, 5] This is process 0 [0, 1, 2, 3, 4, 5, 6] This is the process 0 [0, 1, 2, 3, 4, 5, 6, 7] This is the process 0 [0, 1, 2, 3, 4, 5, 6, 7, 8] This is process 1 [0, 1, 2, 3, 4, 5, 6] This is process 0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] This is the process 0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] This is the process 1 [0, 1, 2, 3, 4, 5, 6, 7] This is process 1 [0, 1, 2, 3, 4, 5, 6, 7, 8] This is process 1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] process 1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [] 0it's okProcess finished with exit code 0
Example 2:
Use Manager
#-*-Coding: UTF-8-*-from multiprocessing import Process, Managerimport timeimport randomdef kkk (a_list, number): for I in range (10): a_list.append (I) time. sleep (random. randrange (2) print ('this is the process {}{}'. format (number, a_list) print ('this is the process {}{}'. format (number, a_list) def jjj (a_list): for I in range (2): process = Process (target = kkk, args = (a_list, I) process. start () if _ name _ = '_ main _': manager = Manager () a_list = manager. list () # a_list = [] process_0 = Process (target = jjj, args = (a_list,) process_0.start () process_0.join () print (a_list) print (len (a_list )) print ('It \'s OK ')
Output:
This is the process 0 [0, 0] This is the process 0 [0, 0, 1] This is the process 0 [0, 0, 1, 2] This is the process 0 [0, 0, 1, 2, 3] This is process 0 [0, 0, 1, 2, 3, 4] This is process 1 [0, 0, 1, 2, 3, 4, 5] This is the process 0 [0, 0, 1, 2, 3, 4, 5, 1] This is the process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6] This is process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2] This is process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3] process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4] This is the process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5]. This is the process 0 [0, 0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6] processes 0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7] process 0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8] This is process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9] This is the process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7] This is the process 0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8] This is the process 0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8] This is the process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8] process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9] process 1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9] [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9] 20it's okProcess finished with exit code 0