Understanding processes
The definition of a process can be defined differently from different perspectives
- A process is an execution of a program
- A process is an activity that occurs when a program and its data are executed sequentially on a processor
- A process is a process that a program with independent functions runs on a data set. It is an independent unit of resource allocation and scheduling in the system
A program is a description of instructions, data and their organizational form, while a process is an entity of the program.
Process is the running process of process entity, an independent unit of system resource allocation and scheduling.
For example:
Program: for example xxx.py this is a program, which is static.
Process: after a program runs, the code + resources used are called process, which is an independent unit of resources allocated by the operating system.
Process status
In work, the number of tasks is often larger than the number of CPU cores, that is, some tasks must be executing while others are waiting for the CPU to execute, resulting in different states
- Ready: Running conditions have been slowed down and are waiting for the CPU to execute
- Running state: the CPU is performing its function
- Wait state: Wait for some condition to be satisfied, such as a program to sleep, then wait state
Python multi-process
GIL(Global Interpreter Lock) : Each thread must obtain the GIL during execution to ensure that only one thread can execute code at a time.
Due to the GIL(global interpreter lock), multi-threading in Python is “pseudo-parallelism” and cannot take advantage of multi-core CPU resources, which is required in most cases in Python if you want to fully utilize multi-core CPU resources. Python provides the multiprocessing module to implement multiple processes.
Multiprocessing Creates multiple processes
The Multiprocessing module is a cross-platform version of the multiprocess module that provides a Process class to represent a Process object, which can be understood as a separate Process that performs other things.
Two while loops are executed together
# -*- coding:utf-8 -*-
import time
from multiprocessing import Process Import the Process class first
def run_proc() :
""" Code to be executed by child process """
while True:
print("-- 2 --")
time.sleep(1)
if __name__=='__main__':
p = Process(target=run_proc) Pass in the target function to be executed
p.start() # start process
while True:
print("-- 1 --")
time.sleep(1)
Copy the code
The running results are as follows:
----1----
----2----
----1----
----2----
----1----
----2----
----1----
----2----
----1----
----2-...Copy the code
instructions
- To create a child Process, you simply pass in an execution function and its parameters, create an instance of Process, and start it with the start() method
Process syntax
Process([group [, target [, name [, args [, kwargs]]]]])
Copy the code
parameter | meaning |
---|---|
target | Represents the calling object, the task to be performed by the child process |
args | The argument passed to the target function as a tuple, as in:args=(1, 2, ‘hui’, ) |
kwargs | Pass the keyword argument to the function specified by target as follows:kwargs={‘name’: ‘hui’, ‘age’: 18} |
name | Give the process a name |
group | Specifying a process group |
Common methods for Process objects
methods | role |
---|---|
start() | Start the child process instance (create the child process) |
is_alive() | Checks whether the process child is still alive |
join([timeout]) | Whether or how many seconds to wait for the child process to finish executing |
terminate() | Terminate the child process immediately, whether the task is complete or not |
Common properties of the Process object
name
: Alias of the current process. The default value isProcess-N
, N is an integer increasing from 1pid
: Pid of current process (process NUMBER)
Passes arguments to the function specified by the child process
# -*- coding:utf-8 -*-
import os
from time import sleep
from multiprocessing import Process
def run_proc(name, age, **kwargs) :
for i in range(10) :print('Child process running, name= %s,age=%d,pid=%d... ' % (name, age, os.getpid()))
print(kwargs)
sleep(0.2)
def main() :
p = Process(target=run_proc, args=('test'.18), kwargs={"m":20})
p.start()
sleep(1) End the child process immediately after 1 second
p.terminate() Terminate the child process
p.join()
if __name__=='__main__':
main()
Copy the code
Running results:
The child process is running. Name = test,age=18 ,pid=45097.. {'m': 20} Child process running, name= test,age=18 ,pid=45097.. {'m': 20} Child process running, name= test,age=18 ,pid=45097.. {'m': 20} Child process running, name= test,age=18 ,pid=45097.. {'m': 20} Child process running, name= test,age=18 ,pid=45097.. {'m': 20}
Copy the code
Inherit the Process class to create multiple processes
You can also create a Process using the class approach. You can create a custom class that inherits the Process class and overrides the run method. Each instantiation of the class is equivalent to instantiating a Process object
import time
from multiprocessing import Process
Define the process class
class ClockProcess(Process) :
def __init__(self,interval) :
super().__init__(self) Remember to load the __init__() method of the Process class
self.interval = interval
def run(self) :
print('Child process start execution time :{}'.format(time.ctime()))
time.sleep(self.interval)
print('Child process end time :{}'.format(time.ctime()))
def main() :
Create child process to sleep for 2 seconds
p = ClockProcess(2)
Call the child process
p.start()
Wait for the child process to finish
p.join()
print('Main process terminated')
if __name__=='__main__':
main()
Copy the code
The results
The time when the child process starts execution :Sat Oct3 23:21:20 2020The end time of the child process :Sat Oct3 23:21:22 2020Main process endCopy the code
Inheriting the Process class and overriding the run method, the instantiated object calls the start() method to execute the code for the run() method
Global variables are not shared between processes
# -*- coding:utf-8 -*-
import os
import time
from multiprocessing import Process
nums = [11.22]
def work1() :
""" Code to be executed by child process """
print("in process1 pid=%d ,nums=%s" % (os.getpid(), nums))
for i in range(3):
nums.append(i)
time.sleep(1)
print("in process1 pid=%d ,nums=%s" % (os.getpid(), nums))
def work2() :
""" Code to be executed by child process """
print("in process2 pid=%d ,nums=%s" % (os.getpid(), nums))
def main() :
p1 = Process(target=work1)
p1.start() The child process is started
p1.join() Wait for the child process to finish executing
p2 = Process(target=work2)
p2.start()
if __name__ == '__main__':
main()
Copy the code
Running results:
in process1 pid=11349 ,nums=[11.22]
in process1 pid=11349 ,nums=[11.22.0]
in process1 pid=11349 ,nums=[11.22.0.1]
in process1 pid=11349 ,nums=[11.22.0.1.2]
in process2 pid=11350 ,nums=[11.22]
Copy the code
Description: p1 adds elements to num list, and P2 does not share them. P2 prints the same num[11, 12].
Python multithreading, process comparison
Compare the way | thread | process |
---|---|---|
Python module | threading |
multiprocessing |
Open means | threading.Thread() inheritance Thread 类 |
multiprocessing.Process() inheritance Process 类 |
Shared global variables | Shared | Do not share |
The public,
Create a new folder X
Nature took tens of billions of years to create our real world, while programmers took hundreds of years to create a completely different virtual world. We knock out brick by brick with a keyboard and build everything with our brains. People see 1000 as authority. We defend 1024. We are not keyboard warriors, we are just extraordinary builders of ordinary world.