What are coroutines in python? (Example analysis)

乌拉乌拉~
Release: 2018-08-23 17:42:00
Original
2002 people have browsed it

In the following article, let’s take a look at what coroutines in python are. Learn about pythoncoroutines and what role coroutines can play in python programming.

python coroutine

The operations of threads and processes are triggered by the program through the system interface, and the final executor is the system; the operation of the coroutine is the programmer.

The significance of the existence of coroutines: For multi-threaded applications, the CPU switches the execution between threads through slicing, and it takes time to switch threads (save the state and continue next time). Coroutines use only one thread to specify the execution order of a certain code block in one thread.

Applicable scenarios for coroutines: When there are a large number of operations (IO) that do not require CPU, it is suitable for coroutines;

event loop is the control point for coroutine execution. If you If you want to execute the coroutine, you need to use them.

Event loop provides the following features:

Registration, execution, cancellation of delayed calls (asynchronous functions)

Create client and server protocols (tools) for communication

Create subprocesses and protocols (tools) that communicate with other programs

Send function calls into the thread pool

Let’s give an example:

Coroutine instance

import asyncio

asyncdef 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()
Copy after login

The last three lines are the focus.

asyncio.get_event_loop() : asyncio starts the default event loop

run_until_complete() : This function is blocking execution until all asynchronous functions are executed,

close () : Close event loop.

The above is all the content described in this article. This article mainly introduces the relevant knowledge of python coroutine. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What are coroutines in python? (Example analysis). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!