In the following article, let's look at what is a
process in Python . Learn about the Python
process and how it can work in Python programming.
Python co-process
The operation of the thread and process is triggered by the program to trigger the system interface, the final performer is the system, and the operation of the coprocessor is the programmer.
The significance of the existence of the process: for multi-threaded applications, the CPU by slicing the way to switch between threads of execution, thread switching takes time (save state, next continue). , only one thread is used, and a code block execution order is specified in one thread.
Application scenario: When there are a large number of operations in the program that do not require the CPU (IO), it is suitable for the association process;
The event loop is the control point for the execution of the process, and you need to use it if you want to perform the co-process.
The event loop provides the following features:
Register, execute, Cancel delay call (Async function)
Create client and server protocols (tools) for communication
Create child processes and protocols that communicate with other programs (tools)
To feed a function call into the thread pool
Let's give an example:
Example of a co-process
Import Asyncioasyncdef Cor1 (): print ("COR1 start") await Cor2 () print ("COR1 end") Asyncdef Cor2 (): print ("COR2") loop = Asyncio.get_event_loop () loop.run_until_complete (Cor1 ()) Loop.close ()
The last three lines are the focus.
Asyncio.get_event_loop (): Asyncio start the default event loop
Run_until_complete (): This function is blocking execution, knowing that all asynchronous functions are executed,
Close (): Closes the event loop.