python-整理 多线程,多进程,线程池,进程池,协程,协程池

本文通过示例代码展示了Python中多线程、多进程、线程池、进程池以及使用gevent的协程和协程池的实现方式,探讨了它们在并发执行任务时的性能和适用场景。代码运行时可能导致电脑卡顿,提示注意资源管理。
摘要由CSDN通过智能技术生成

记录下多线程,多进程,线程池,进程池,协程,协程池.

有可能是配置不行,我把下面的代码全部同时运行 电脑就卡住了.

运行函数

def run(max):
    import random
    import time
    time.sleep(random.uniform(0, 1))
    print(max, end="\n")


list_Url = [item for item in range(100)]

多线程

 # 多线程-------------------------
if __name__ == '__main__':
    import threading

    Thread_list = []
    for item in list_Url:
        t1 = threading.Thread(target=run, args=(item,))
        Thread_list.append(t1)
        t1.start()
    for item_1 in Thread_list:
        item_1.join()

多进程

# 多进程-------------------------
if __name__ == '__main__':
    
    from multiprocessing import Process

    Thread_list = []
    Process_list = []
    for item in list_Url:
        t1 = Process(target=run, args=(item,))
        Process_list.append(t1)
        t1.start()
    for item_1 in Process_list:
        item_1.join()

线程池

# 线程池-------------------------
if __name__ == '__main__':
    from concurrent.futures import ThreadPoolExecutor

    with ThreadPoolExecutor(max_workers=32) as executor:
        executor.map(run, list_Url, timeout=None)
     
    # --------------------------------------------
    from concurrent.futures import ThreadPoolExecutor

    thread_pool = ThreadPoolExecutor(max_workers=200)
    for i in range(11000):
        thread_pool.submit(run, i)
    thread_pool.shutdown(wait=True)
	
	#----------------------------
	import concurrent.futures

    # 这是线程池的方式
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        futurelist = []
        for i in range(10):  # 模拟多个任务
            future = executor.submit(run, i)
            futurelist.append(future)

        for future in concurrent.futures.as_completed(futurelist):  # 并发执行
            print(future.result())    # 拿到结果

进程池

# 进程池-------------------------
if __name__ == '__main__':
    from multiprocessing import Pool

    with Pool(5)as  mp:
        mp.map(run, list_Url)

协程 gevent

#  协程 gevent-------------------------
if __name__ == '__main__':
    import gevent
    from gevent import monkey

    monkey.patch_all()
    monkey.patch_socket()
    list_a = []
    for item in [item for item in range(100)]:
        list_a.append(gevent.spawn(run, item))
    gevent.joinall(list_a)

协程 gevent pool

# 协程 gevent pool-------------------------
if __name__ == '__main__':
    import gevent
    from gevent import monkey
    from gevent.pool import Pool

    monkey.patch_all()
    list_a = []  # 限制最大并发
    pool = Pool(66)  # 分配100个任务,最大并发数为66
    for i in range(100):
        list_a.append(pool.spawn(run, i))
    gevent.joinall(list_a)

全部写到一起

def run(max):
    import random
    import time
    time.sleep(random.uniform(0, 1))
    print(max, end="\n")


list_Url = [item for item in range(100)]

if __name__ == '__main__':
    # 多线程-------------------------
    import threading

    Thread_list = []
    for item in list_Url:
        t1 = threading.Thread(target=run, args=(item,))
        Thread_list.append(t1)
        t1.start()
    for item_1 in Thread_list:
        item_1.join()

    # 多进程-------------------------
    from multiprocessing import Process

    Thread_list = []
    Process_list = []
    for item in list_Url:
        t1 = Process(target=run, args=(item,))
        Process_list.append(t1)
        t1.start()
    for item_1 in Process_list:
        item_1.join()

    # 线程池-------------------------
    from concurrent.futures import ThreadPoolExecutor

    with ThreadPoolExecutor(max_workers=32) as executor:
        executor.map(run, list_Url, timeout=None)

    # 进程池-------------------------
    from multiprocessing import Pool

    with Pool(5)as  mp:
        mp.map(run, list_Url)
    # 协程 gevent-------------------------
    import gevent
    from gevent import monkey

    monkey.patch_all()
    monkey.patch_socket()
    list_a = []
    for item in [item for item in range(100)]:
        list_a.append(gevent.spawn(run, item))
    gevent.joinall(list_a)

    #  协程 gevent pool-------------------------
    import gevent
    from gevent import monkey
    from gevent.pool import Pool

    monkey.patch_all()
    list_a = []  # 限制最大并发
    pool = Pool(66)  # 分配100个任务,最大并发数为66
    for i in range(100):
        list_a.append(pool.spawn(run, i))
    gevent.joinall(list_a)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值