Python多进程+多线程+协程同时应用

需求:10000个请求,开启2个进程,进程中开启3个线程,线程中开启5个协程来处理 。

⭐️:多线程里开启协程,不建议使用猴子补丁,直接用gevent.sleep(0.001)

"""10000个请求,开启2个进程,进程中开启3个线程,线程中开启5个协程来处理

"""

import requests, time
from multiprocessing import Queue, Process
import threading
import gevent


def process_work(q, p_name):
    """
    创建3个线程
    :param q: 队列
    :param pname: 进程名
    :return:
    """
    thread_list = []
    for i in range(3):
        t_name = "{}--t--{}".format(p_name, i)
        t = threading.Thread(target=thread_work, args=(q, t_name))
        print('创建线程---{}'.format(t_name))
        t.start()
        thread_list.append(t)
    for thread in thread_list:
        thread.join()


def thread_work(q, t_name):
    """
    创建5个协程
    :param q: 队列名
    :param tname: 线程名
    :return:
    """
    g_list = []
    for i in range(5):
        g_name = "{}--g--{}".format(t_name, i)  # 协程名
        print("创建协程----{}".format(g_name))
        g = gevent.spawn(gevent_work, q, g_name)
        g_list.append(g)
    gevent.joinall(g_list)


def gevent_work(q, g_name):
    """
    协程做的事:处理任务
    :param q: 队列
    :param gname: 协程名
    :return:
    """
    count = 0
    while not q.empty():
        url = q.get(timeout=0.01)
        requests.get(url=url)
        gevent.sleep(0.01)
        count += 1
    print("----协程{}执行了{}个任务".format(g_name, count))


def count_time(old_func):
    """函数计时装饰器"""

    def wrapper(*args, **kwargs):
        print('开始执行')
        st = time.time()
        old_func(*args, **kwargs)
        et = time.time()
        print('结束执行')
        print('执行耗时:{}'.format(et - st))

    return wrapper


@count_time
def main():
    """
    main函数创建2个进程,控制程序的运行
    :return:
    """
    q = Queue()
    # 创建10000个任务在队中
    for i in range(10000):
        q.put("http://127.0.0.1:5000/demo")
    print("11111")
    # print(q.qsize())  #TODO执行就报错?
    process_list = []
    # 将创建的进程都加入进程列表,并启动
    for i in range(2):
        p_name = 'p--{}'.format(i)
        print('创建进程-{}'.format(p_name))
        pro = Process(target=process_work, args=(q, p_name))  # 进程不共享全局变量,所有q做参数传进去
        process_list.append(pro)
        pro.start()
    # 主进程等待所有子进程
    for pro in process_list:
        pro.join()


if __name__ == '__main__':
    main()


输出:
开始执行
11111
创建进程-p--0
创建进程-p--1
创建线程---p--0--t--0
创建协程----p--0--t--0--g--0
创建线程---p--0--t--1
创建协程----p--0--t--1--g--0
创建线程---p--0--t--2
创建协程----p--0--t--2--g--0
创建线程---p--1--t--0
创建协程----p--1--t--0--g--0
创建协程----p--0--t--1--g--1
创建协程----p--0--t--1--g--2
创建协程----p--0--t--1--g--3
创建协程----p--0--t--1--g--4
创建线程---p--1--t--1
创建协程----p--1--t--1--g--0
创建线程---p--1--t--2
创建协程----p--1--t--2--g--0
创建协程----p--1--t--0--g--1
创建协程----p--1--t--0--g--2
创建协程----p--1--t--1--g--1
创建协程----p--1--t--1--g--2
创建协程----p--1--t--2--g--1
创建协程----p--1--t--2--g--2
创建协程----p--0--t--0--g--1
创建协程----p--0--t--0--g--2
创建协程----p--0--t--0--g--3
创建协程----p--0--t--0--g--4
创建协程----p--0--t--2--g--1
创建协程----p--0--t--2--g--2
创建协程----p--0--t--2--g--3
创建协程----p--0--t--2--g--4
创建协程----p--1--t--2--g--3
创建协程----p--1--t--2--g--4
创建协程----p--1--t--0--g--3
创建协程----p--1--t--0--g--4
创建协程----p--1--t--1--g--3
创建协程----p--1--t--1--g--4
----协程p--0--t--0--g--3执行了331个任务
----协程p--1--t--1--g--3执行了334个任务
----协程p--1--t--1--g--4执行了334个任务
----协程p--0--t--0--g--4执行了331个任务
----协程p--1--t--2--g--1执行了333个任务
----协程p--0--t--0--g--0执行了332个任务
----协程p--1--t--0--g--3执行了332个任务
----协程p--1--t--2--g--2执行了333个任务
----协程p--1--t--0--g--4执行了332个任务
----协程p--0--t--2--g--0执行了334个任务
----协程p--0--t--2--g--1执行了334个任务
----协程p--0--t--2--g--2执行了334个任务
----协程p--0--t--1--g--0执行了334个任务
----协程p--0--t--1--g--1执行了334个任务
----协程p--1--t--1--g--0执行了335个任务
----协程p--1--t--2--g--3执行了333个任务
----协程p--0--t--0--g--1执行了332个任务
----协程p--0--t--1--g--2执行了334个任务
----协程p--1--t--0--g--0执行了333个任务
----协程p--1--t--1--g--1执行了335个任务
----协程p--0--t--2--g--3执行了334个任务
----协程p--1--t--2--g--4执行了333个任务
----协程p--1--t--0--g--1执行了333个任务
----协程p--0--t--1--g--3执行了334个任务
----协程p--0--t--0--g--2执行了332个任务
----协程p--1--t--1--g--2执行了335个任务
----协程p--1--t--2--g--0执行了334个任务
----协程p--0--t--2--g--4执行了334个任务
----协程p--1--t--0--g--2执行了333个任务
----协程p--0--t--1--g--4执行了334个任务
结束执行
执行耗时:6.8051958084106445

  • 3
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chuntian_tester

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值