Python multi-coroutine development (microthread, fiber)

Python has multiple processes, multiple threads and multiple coroutines for concurrent processing

In the concurrent programming provided by python, the development of multi-coroutine is the highest performance form of implementation. Multithreading has the problem of GIL lock;
the resource control of multi-coroutine is handed over to the developer to control, multi-process and multi- Threads are controlled by the operating system. There will be various rotations. After a period of execution, resources will be surrendered and handed over to other processes or threads for processing. Such processing will have performance costs;

Insert picture description here

1. Yeild realizes multi-coroutine

There is no system-level processing control for the current program development.
If you forget how to use yield, please click here to
refer to Liao Xuefeng’s

# 生产者
def producer(cons):
    # 生产数据
    info = None;
    cons.send(info);
    for item in range(10):
        if item % 2 ==0:
            info = "title = 奥特曼,content = 奥特曼打怪兽";
        else:
            info = "title = 黑猫警长,content = 黑猫警长抓老鼠";
        print("【生产者:{}】".format(info));
        cons.send(info);

def consumer():
    while True:
        # 等待进行数据的接受
        receive = yield ;
        # 消费者输出信息
        print("【消费者:{}】".format(receive));
def main():

    con = consumer();
    producer(con);

if __name__ == '__main__':
    main();

2.greenlet is a multi-threaded development module support provided in python. You need to manually download
pip install greenlet.
The following code can see that there is no need for send and yield to operate;

import greenlet,time
info = None;
def producer_handle():
    global info;
    for item in range(10):
        if item % 2 == 0:
            info = "title = 奥特曼,content = 奥特曼打小怪兽";
        else:
            info = "title = 黑猫警长,content = 黑猫警长抓小老鼠";
        print("【生产者】:{}".format(info));
        # 模拟业务逻辑处理
        time.sleep(1);
        # 切换到消费端;
        consumer_greenlet.switch();

def consumer_handle():
    while True:
        print("【消费者】:{}".format(info));
        time.sleep(1);
        # 切换到生产端
        producer_greenlet.switch();
# 定义协程切换函数
producer_greenlet = greenlet.greenlet(run=producer_handle);
# 定义协程切换函数
consumer_greenlet = greenlet.greenlet(run=consumer_handle);

def main():
    producer_greenlet.switch();

if __name__ == '__main__':
    main();

3. The difference between gevent third-party module and greenlet.
Greenlet needs to manually switch production and consumption, and gevent automatically switches. You
need to manually download pip install gevent.
Below is the sample code. Because it is while true, it will always consume the last message

import gevent
info = None;
def producer_handle():
    global info;
    for item in range(10):
        if item % 2 == 0:
            info = "title = 奥特曼,content = 奥特曼打小怪兽 ,{}".format(item);
        else:
            info = "title = 黑猫警长,content = 黑猫警长抓小老鼠 ,{}".format(item);
        print("【生产者】:{}".format(info));
        # 切换延迟
        gevent.sleep(1);

def consumer_handle():
    while True:
        print("【消费者】:{}".format(info));
        # 切换延迟
        gevent.sleep(1);

def main():
    # 定义协程对象
    producer_gevent = gevent.spawn(producer_handle);
    # 定义协程对象
    consumer_gevent = gevent.spawn(consumer_handle);
    producer_gevent.join();
    consumer_gevent.join();

if __name__ == '__main__':
    main();

In fact, you can find that python is constantly improving itself and introducing some new features. For novices and people who do not understand computer hardware, it is becoming more and more friendly, writing less and doing more;

Guess you like

Origin blog.csdn.net/weixin_44887276/article/details/114882099