In-depth understanding of Python coroutines

1. Introduction to coroutines

Coroutine (Coroutine), also known as micro-thread, fiber. The English name is Coroutine. A coroutine is a lightweight thread in user mode, and the scheduling of the coroutine is completely controlled by the user. A coroutine has its own register context and stack. When the coroutine schedule is switched, save the register context and stack to other places, and restore the previously saved register context and stack when switching back. Directly operating the stack basically has no kernel switching overhead, and can access the global without locking Variables, so context switching is very fast.

There are many implementations of coroutines in Python, including but not limited to generators, greenlets, asyncio libraries, etc. After Python 3.5, Python added new syntax asyncand await, making the writing of coroutines easier. Here is a simple coroutine example:

python
复制代码
async def hello():
    print("Hello world!")
    await asyncio.sleep(1)
    print("Hello again!")

# Get a reference to the event loop as we plan to use
# low-level APIs.
loop = asyncio.get_event_loop()

# "hello" coroutine returns immediately because it is await'ed instantly.
#
# Therefore, "hello_task" is scheduled to run soon.
hello_task = loop.create_task(hello())

# "gather" returns when all the scheduled tasks are done.
loop.run_until_complete(hello_task)

2. The relationship between coroutines and generators

In Python, coroutines and generators are closely related. In fact, coroutines are implemented through generators. When we use yieldstatements, we are creating a generator. And when we use yield fromstatements, we are creating a coroutine.

python
复制代码
# A generator function
def countdown(n):
    while n > 0:
        yield n
        n -= 1

# A coroutine function
def countup(n):
    x = 0
    while x < n:
        yield from countdown(x)
        x += 1

In the above code, countdownis a generator function, countupbut a coroutine function. countupThe function generates a series of numbers, from 0 to n-1. Each time it is called countup, it calls countdownthe generator, and waits for it to complete.

3. Advantages and application scenarios of coroutines

Coroutines have many advantages, mainly in the following aspects:

  • Small resource overhead: Coroutines are scheduled in user mode and do not involve system calls. That is to say, the resources consumed by creating, switching, and destroying coroutines are far less than processes and threads.
  • The code is concise and easy to read: Code written using coroutines can avoid callback hell, making the code more concise and the logic clearer. At the same time, the design of coroutines makes it easier for us to deal with problems in concurrent and parallel programming.
  • Efficient IO operations: The most commonly used scenario for coroutines is IO operations. Coroutines can actively transfer control rights during IO operations, thereby avoiding unnecessary blocking and waiting.

Let's see an example of using coroutines to handle IO:

python
复制代码
async def download(url):
    response = await aiohttp.request('GET', url)
    return await response.text()

async def download_all(urls):
    tasks = [download(url) for url in urls]
    return await asyncio.gather(*tasks)

urls = ['http://example.com', 'http://example.org', 'http://example.net']
loop = asyncio.get_event_loop()
htmls = loop.run_until_complete(download_all(urls))

In this example, downloadthe coroutine downloads a web page asynchronously and returns its text content. download_allThe coroutine downloads a series of web pages asynchronously and returns a list containing all text content.

Coroutines are usually used in the following scenarios:

  • Web crawler: Through coroutines, we can download other web pages while downloading a web page, which greatly improves the efficiency of crawlers.
  • Real-time data processing: Coroutines can process real-time data streams, such as stock data, social media data, etc.
  • Web servers: Python web servers like Tornado and Sanic are implemented with coroutines.

4. How to use coroutines correctly

After understanding the principles and advantages of coroutines, let's take a look at how to use coroutines correctly.

First, we need to create the coroutine object. Coroutine objects are created through coroutine functions. A coroutine function is async defa function containing keywords:

python
复制代码
async def my_coroutine():
    return 123

Then, we need to drive the coroutine through the event loop:

python
复制代码
coro = my_coroutine()
loop = asyncio.get_event_loop()
loop.run_until_complete(coro)

run_until_completemethod blocks the current thread until the passed coroutine finishes running.

In a coroutine, we can use awaitthe keyword to wait for the completion of other coroutines. For example, the following code creates a coroutine that first waits for asyncio.sleep(1)the coroutine and then prints "Hello, world":

python
复制代码
async def hello():
    await asyncio.sleep(1)
    print("Hello, world!")

This is the basic usage of coroutines in Python. Coroutines are the core of Python asynchronous programming. After understanding coroutines, we can better understand and use Python's asynchronous programming features.

If you are interested in Python and want to get a higher salary by learning Python, then the following set of Python learning materials must be useful to you!

Materials include: Python installation package + activation code, Python web development, Python crawler, Python data analysis, artificial intelligence, machine learning and other learning tutorials. Even beginners with 0 basics can understand and understand. Follow the tutorial and take you to learn Python systematically from zero basics!

1. Learning routes in all directions of Python

The route of all directions in Python is to organize the commonly used technical points of Python to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.
insert image description here
2. Python learning software

If a worker wants to do a good job, he must first sharpen his tools. The commonly used development software for learning Python is here!
insert image description here
3. Python introductory learning video

There are also many learning videos suitable for getting started with 0 basics. With these videos, you can easily get started with Python~insert image description here

4. Python exercises

After each video lesson, there are corresponding practice questions, you can test the learning results haha!
insert image description here

Five, Python actual combat case

Optical theory is useless. You have to learn to type codes along with it, and then you can apply what you have learned in practice. At this time, you can learn from some practical cases. This information is also included~insert image description here

6. Python interview materials

After we have learned Python, we can go out and find a job with the skills! The following interview questions are all from top Internet companies such as Ali, Tencent, Byte, etc., and some Ali bosses have given authoritative answers. After reading this set of interview materials, I believe everyone can find a satisfactory job.
insert image description here
insert image description here
7. Information collection

The full set of learning materials for the above-mentioned full version of Python has been uploaded to the CSDN official website. Those who need it can scan the QR code of the CSDN official certification below on WeChat to receive it for free.

Supongo que te gusta

Origin blog.csdn.net/pythonhy/article/details/132147862
Recomendado
Clasificación