What is a co-process in Python? (Instance parsing)

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.