记录下多线程,多进程,线程池,进程池,协程,协程池.
有可能是配置不行,我把下面的代码全部同时运行 电脑就卡住了.
运行函数
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)