小练习,假设一个队列中有100000个URL地址,每个请求需要1秒钟,尝试用4个进程,每个进程中开启1000个协程去请求!统计运行时间
from gevent import monkey
monkey.patch_all(thread=False)
import gevent
import time
from multiprocessing import Process, Queue
import os
def time_count(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
end_time = time.time()
print('总耗时:', end_time - start_time)
return wrapper
class Myprocess(Process):
def __init__(self, que):
super().__init__()
self.que = que
#重写进程子类的run函数
def run(self):
cos = []
#开启多协程
for i in range(1000):
#调用工作函数
cor = gevent.spawn(self.work)
cos.append(cor)
gevent.joinall(cos)
#定义工作函数
def work(self):
while self.que.qsize() > 0:
try:
url = self.que.get(timeout=1)
time.sleep(1)
print(f"{os.getpid()}正在请求url:{url}")
except Exception as e:
print(e.__repr__())
break
@time_count
def main():
q = Queue()
for i in range(100000):
q.put(f'https://www.baidu.com--{i}')
process_list = []
for i in range(4):
p = Myprocess(q)
process_list.append(p)
p.start()
for p in process_list:
p.join()
print("任务结束")
if __name__ == '__main__':
main()
运行时间27秒
网友评论