process
Because of the GIL, multithreading in Python is not really multithreading, and in most cases it is necessary to use multiple processes if you want to make full use of multi-core CPU resources. Python provides a very useful multiprocess module called Multiprocessing. You only need to define a function and Python does everything else. With this module, the transition from single-process to concurrent execution can be easily accomplished. Multiprocessing supports child processes, communication and sharing of data, various forms of synchronization, and provides components such as Process, Lock, Queue, and Pipe.
The multiprocessing module is a multi-process module in Python. Similar to threading.Thread, it can create a Process using the Multiprocessing. Process object. This process can run functions written inside Python programs. The Process object is used in the same way as the Thread object, and also has the start(),run(), and join() methods. In addition multiprocessing module also has the Lock/Event/Semaphore/Condition class (these objects can be like a multi-threaded, through the parameters passed to the process), to the synchronization process, the usage of the same name with the threading module class. So a large part of Multiprocessing uses the same API as threading, but in a multi-process context. Of course, multi-process is defined in a similar way to multi-threading, namely in two ways:
Object function instantiation defines a new process:
# Import multi-process module
from multiprocessing import Process
# os.getpid() gets the id of the current process
import os
def run_proc(name):
print('{} child process is {}'.format(name, os.getpid()))
if __name__ == '__main__':
print("Parent process is {}".format(os.getpid()))
p = Process(target=run_proc, args=('test', ))
print('child process will start... ')
p.start()
p.join()
print('child process end.')
Copy the code
The operation screenshot is as follows:
Inherit classes to define new processes
from multiprocessing import Process
import os
class RunProc(Process):
def __init__(self, name):
Process.__init__(self)
self.name = name
def run(self):
print('{} child process is {}'.format(self.name, os.getpid()))
if __name__ == "__main__":
print("Parent process is {}".format(os.getpid()))
p = RunProc('test')
print('child process will start... ')
p.start()
p.join()
print('child process end.')
Copy the code
The running results are as follows:
- Group: thread group, not yet implemented, must be None in library references;
- Target: the method to execute;
- Name: indicates the process name.
- Args /kwargs: Parameters to be passed to the method.
Instance methods:
- Is_alive () : returns whether the process is running.
- Join ([timeout]) : Blocks the process in the current context until the process calling this method terminates or reaches the specified timeout (optional).
- Start () : The process is ready for CPU scheduling
- Run () : Strat () calls the run method if the instance process is not passed in
target
.start
Execute the default ·run()
Methods. - Terminate () : Immediately stop the work process whether or not the task is complete
Properties:
- Daemon: and threads
setDeamon
Function as - Exitcode (the process is None at run time or terminated by signal N if it is -n)
- Name: indicates the process name.
- Pid: indicates the process ID.
Process independence:
Unlike threads, processes are independent of each other, and we can get a glimpse of this by modifying global variables:
from multiprocessing import Process
# Test data
ex_list = 'Hello World'
Modify the data process
def revise_data(a):
global ex_list
Modify global variables
ex_list = ex_list + ' with write revise_data process.'
print('wirte result:', ex_list)
# View the data process
def view_data(a):
print(ex_list)
if __name__ == "__main__":
process_revise = Process(target=revise_data)
process_view = Process(target=view_data)
process_revise.start()
The main process waits for the writing process to complete before continuing to execute
process_revise.join()
process_view.start()
process_view.join()
print("process end.")
Copy the code
The operation screenshot is as follows: