Tomorrow源代码
项目地址
作者madisonmay
from functools import wraps
from concurrent.futures import ThreadPoolExecutor
class Tomorrow():
def __init__(self, future, timeout):
self._future = future
self._timeout = timeout
self._wait = self._future.result
def __getattr__(self, name):
result = self._future.result(self._timeout)
return result.__getattribute__(name)
def async(n, base_type, timeout=None):
def decorator(f):
if isinstance(n, int):
pool = base_type(n)
elif isinstance(n, base_type):
pool = n
else:
raise TypeError(
"Invalid type: %s"
% type(base_type)
)
@wraps(f)
def wrapped(*args, **kwargs):
return Tomorrow(
pool.submit(f, *args, **kwargs),
timeout=timeout
)
return wrapped
return decorator
def threads(n, timeout=None):
return async(n, ThreadPoolExecutor, timeout)
示范代码
(虽然不如gevent方便,不如multiprocessing.dummy性能高,但是至少有了进度条功能
import time
import requests
import sys
from tomorrow import threads
s=requests.Session()
jishu=0
@threads(10)
def download(url):
global jishu
for _ in range(5):
try:
jishu+=1
aa=s.get(url)
sys.stderr.write(‘%s \r‘%jishu)
except:
pass
return aa
urls = [‘http://p.3.cn/prices/mgets?skuIds=J_1273600‘]*100
if __name__ == "__main__":
start = time.time()
responses = [download(url) for url in urls]
html = [response.text for response in responses]
end = time.time()
print ("Time: %f seconds" % (end - start))
时间: 2024-12-27 23:26:44