Home Backend Development Python Tutorial What is the use of coroutines and concurrency in python?

What is the use of coroutines and concurrency in python?

Sep 21, 2018 pm 03:19 PM
gevent python coroutine

This chapter will introduce you to the role of coroutines and concurrency in python, so that you can understand the pros and cons of using coroutines, and the role of the gevent concurrency framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Coroutine

Coroutine is a lightweight thread in user mode, also known as micro-thread.

The coroutine has its own register context and stack. When the schedule is switched, the register context and stack are saved elsewhere. When switching back, the previously saved register context and stack are restored. Therefore: the coroutine can retain the state of the last call (that is, a specific combination of all local states). Each time the process re-enters, it is equivalent to entering the state of the last call. In other words: entering the state when it left last time. The location of the logical flow.

Advantages:

  1. No overhead of thread context switching

  2. No overhead of atomic operation locking and synchronization

  3. Conveniently switch control flow and simplify the programming model

  4. High concurrency, high scalability and low cost: it is not a problem for a CPU to support tens of thousands of coroutines. So it is very suitable for high concurrency processing.

The so-called atomic operation refers to an operation that will not be interrupted by the thread scheduling mechanism; once this operation starts, it will run until the end without any context switch (switch to another thread).

Atomic operations can be one step or multiple steps, but the order cannot be disrupted or only the executed part can be cut off. Seeing as a whole is at the heart of atomicity.

Disadvantages:

  1. Unable to utilize multi-core resources: The essence of the coroutine is a single thread. It cannot use multiple cores of a single CPU at the same time. The coroutine needs to be Processes must cooperate to run on multiple CPUs. Of course, most of the applications we write daily do not have this necessity, unless they are CPU-intensive applications.

  2. Performing blocking operations (such as IO) will block the entire program

Use Gevent

gevent is a concurrency framework for python, with micro-thread greenlet as the core, using the epoll event listening mechanism and many other optimizations to become efficient.:

  • Simple example

gevent's sleep can hand over control. When we use gevent in functions that are restricted by network or IO, these functions will be scheduled cooperatively. The true capabilities of gevent will be unleashed. Gevent handles all the details to ensure that your network library will implicitly hand over execution rights to the greenlet context when possible.

import gevent
 
def foo():
    print('running in foo')
    gevent.sleep(0)
    print('com back from bar in to foo')
 
def bar():
    print('running in bar')
    gevent.sleep(0)
    print('com back from foo in to bar')
 
# 创建线程并行执行程序
gevent.joinall([
    gevent.spawn(foo),
    gevent.spawn(bar),
])
Copy after login

Execution result:

What is the use of coroutines and concurrency in python?

  • Synchronous asynchronous

import random
import gevent
 
def task(pid):
    gevent.sleep(random.randint(0, 2) * 0.001)
    print('Task %s done' % pid)
 
def synchronous():
    for i in range(1, 10):
        task(i)
 
def asynchronous():
    threads = [gevent.spawn(task, i) for i in range(10)]
    gevent.joinall(threads)
 
print('Synchronous:')
synchronous()
 
print('Asynchronous:')
asynchronous()
Copy after login

Execution output:

What is the use of coroutines and concurrency in python?

  • Using coroutines in a subclass method

You can subclass the Greenlet class. Overload its _run method, similar to multi-thread and multi-process modules

import gevent
from gevent import Greenlet
 
class Test(Greenlet):
 
    def __init__(self, message, n):
        Greenlet.__init__(self)
        self.message = message
        self.n = n
 
    def _run(self):
        print(self.message, 'start')
        gevent.sleep(self.n)
        print(self.message, 'end')
 
tests = [
    Test("hello", 3),
    Test("world", 2),
]
 
for test in tests:
    test.start()  # 启动
 
for test in tests:
    test.join()  # 等待执行结束
Copy after login
  • Use monkey patch to modify the system standard library (automatically switch coroutines)

When a greenlet encounters an IO operation, such as accessing the network, it will automatically switch to other greenlets, wait until the IO operation is completed, and then switch back at the appropriate time to continue execution.

Since IO operations are very time-consuming, the program is often placed in a waiting state. With gevent automatically switching coroutines for us, it is guaranteed that greenlets are always running instead of waiting for IO.

Since switching is automatically completed during IO operations, gevent needs to modify some of the standard libraries that come with Python. This process is completed through monkey patch at startup

import gevent
import requests
from gevent import monkey
 
monkey.patch_socket()
 
def task(url):
    r = requests.get(url)
    print('%s bytes received from %s' % (len(r.text), url))
 
gevent.joinall([
    gevent.spawn(task, 'https://www.baidu.com/'),
    gevent.spawn(task, 'https://www.qq.com/'),
    gevent.spawn(task, 'https://www.jd.com/'),
])
Copy after login

Execution output:

What is the use of coroutines and concurrency in python?

It can be seen that the three network operations are executed concurrently, and the ending order is different

The above is the detailed content of What is the use of coroutines and concurrency in python?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Can Python parameter annotations use strings? Can Python parameter annotations use strings? Apr 01, 2025 pm 08:39 PM

Alternative usage of Python parameter annotations In Python programming, parameter annotations are a very useful function that can help developers better understand and use functions...

Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Apr 01, 2025 pm 05:24 PM

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How do Python scripts clear output to cursor position at a specific location? How do Python scripts clear output to cursor position at a specific location? Apr 01, 2025 pm 11:30 PM

How do Python scripts clear output to cursor position at a specific location? When writing Python scripts, it is common to clear the previous output to the cursor position...

Python hourglass graph drawing: How to avoid variable undefined errors? Python hourglass graph drawing: How to avoid variable undefined errors? Apr 01, 2025 pm 06:27 PM

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

Why can't my code get the data returned by the API? How to solve this problem? Why can't my code get the data returned by the API? How to solve this problem? Apr 01, 2025 pm 08:09 PM

Why can't my code get the data returned by the API? In programming, we often encounter the problem of returning null values ​​when API calls, which is not only confusing...

See all articles