SoFunction
Updated on 2025-03-05

Sample code for multi-process implementation of data sharing in python

background

The Android UI automation framework uses multi-device parallelism implemented by multiple processes. When collecting data for data summary, multiple processes need to share data.

Process, process creation

If the program is not run after writing it, it is called a program. The running code is the process. In Python 3 language, multiprocessing modules and subprocess modules are supported for multiprocessing. The multiprocessing module provides support for running tasks, communicating and sharing data in child processes, and performing various forms of synchronization.

Python provides a very useful multiprocessing package multiprocessing. You only need to define a function, and Python will do everything else. With this package, the conversion from a single process to a concurrent execution can be easily completed. multiprocessing supports child processes, communication and sharing data. The syntax format is as follows:

Process([group [, target [, name [, args [, kwargs]]]]])

Inter-process communication

Multithreading: Sharing variables are very simple, just define the global global variable directly

Multi-process: Global variables are not shared in multiple processes, and the data between processes are independent and do not affect each other by default.

from multiprocessing import Process
num=1
def work1():
    global num
    num+=5
    print('Subprocess 1 runs, num:',num)

def work2():
    global num
    num += 10
    print('Subprocess 2 runs, num:',num)


if __name__=='__main__':
    print('The parent process starts running')
    p1=Process(target=work1)
    p2=Process(target=work2)
    ()
    ()
    ()
    ()

Execution results

The parent process starts running
Child process 1 runs, num: 6
Child process 2 runs, num: 11

  • But python also provides such a standard library. The manager module of the process process and the manager module of the pool process.

Share data between processes

  • Share numerical data

The main process and the child process share a value

import multiprocessing


def func(num):
     = 2  


if __name__ == "__main__":
    num = ("d", 1) 
    p = (target=func, args=(num,))
    ()
    ()
  • Share array data

The child process changes dict, list, and the main process dict, list changes as follows

Shared list practice

Now I want to take out the tuples of the automated running results of each mobile phone to analyze them and use the shared list method

# In multiple processes, each of the same variables has a copy of each process, and has no effect on each other;    # In multi-threading, all variables are shared by all threads, and any variable can be modified by any thread.    # ()Implement data sharing in multiple processes    RESULT_TIMEPARAM = ().list()
    RESULT_REPORT_NAME_LIST = ().list()
    RESULT_TUPLE_LIST = ().list()
    devices_pool = []
    for tmp_udid in ConfigData().mobile_data().keys():
        devices_pool.append((target=run_testcase, args=(tmp_udid,RESULT_TIMEPARAM,RESULT_REPORT_NAME_LIST,
                                                                               RESULT_TUPLE_LIST )))

    for devices_udid in devices_pool:
        devices_udid.start()

    for devices_udid in devices_pool:
        devices_udid.join()

The resulting RESULT_TUPLE_LIST:

[('2022-10-11 09:59:46', 'Total 1, pass 1, pass rate = 100.00%', 'Android', '7.1.2', 'samsung', '3.9.3.018'), ('2022-10-11 09:59:47', 'Total 1, pass 1, pass rate = 100.00%', 'Android', '10', 'OPPO', '3.9.3.018')]

This is the article about the example code for python multi-process data sharing. For more related python multi-process data sharing content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!