def LoopRequests():
count = 0
t = []
while True:
if count > 1:
break
s = time.time()
req = requests.get(url)
e = time.time()
# time.sleep(0.1)
await asyncio.sleep(.1)
print(e-s)
# single process thread t.append(e - s)
es = 1 if e - s > 5 else 0
red.rpush('x1', es)
count += 1
return t
import urllib
from urllib import request
def LoopUrlib():
count = 0
t = []
while True:
if count > 1:
break
s = time.time()
req = request.Request(url=url)
rep = request.urlopen(req)
e = time.time()
# time.sleep(0.1)
await asyncio.sleep(.1)
print(">>", e-s)
#single process thread t.append(e - s)
es = 1 if e - s > 5 else 0
red.rpush('x2', es)
count += 1
return t
import multiprocessing
from multiprocessing import Pool
def multiPool(mode = ''):
if mode == 'Loopreuqests':
st = time.time()
pool = Pool(10)
for i in range(100):
pool.apply_async(LoopRequests)
pool.close()
pool.join()
print('>>>', time.time()-st)
elif mode == 'LoopUrlib':
st = time.time()
pool = Pool(10)
for i in range(100):
pool.apply_async(LoopUrlib)
def threadTest(mode):
import threading
if mode == 'Loopreuqests':
st = time.time()
th = []
for i in range(5):
th.append(threading.Thread(target=LoopRequests))
for i in th:
i.start()
i.join()
print('>>>', time.time() - st)
elif mode == 'LoopUrlib':
st = time.time()
th = []
for i in range(5):
th.append(threading.Thread(target=LoopUrlib))
for i in th:
i.start()
i.join()
print('>>>', time.time() - st)
def get(k):
a = []
while True:
try:
x = red.lpop(k)[1]
if x == None:raise ValueError
a.append(x)
except Exception as e:
print(e)
# break
return a
import gevent
from gevent import monkey;monkey.patch_all()
def GeventTest():
s = time.time()
gt = [gevent.spawn(LoopUrlib) for i in range(1000)]
gevent.joinall(gt)
d = time.time()
# print("gevent:", d-s)
gt = [gevent.spawn(LoopRequests) for i in range(1000)]
gevent.joinall(gt)
print("gevent:", time.time() - d)
---------------------
转载,仅作分享,侵删
作者:hea_gui_lion
原文:https://blog.csdn.net/hea_gui_lion/article/details/85884347