Python3使用tomorrow异步

Tomorrow源代码

项目地址
作者madisonmay

  1. from functools import wraps
  2. from concurrent.futures import ThreadPoolExecutor
  3. class Tomorrow():
  4. def __init__(self, future, timeout):
  5. self._future = future
  6. self._timeout = timeout
  7. self._wait = self._future.result
  8. def __getattr__(self, name):
  9. result = self._future.result(self._timeout)
  10. return result.__getattribute__(name)
  11. def async(n, base_type, timeout=None):
  12. def decorator(f):
  13. if isinstance(n, int):
  14. pool = base_type(n)
  15. elif isinstance(n, base_type):
  16. pool = n
  17. else:
  18. raise TypeError(
  19. "Invalid type: %s"
  20. % type(base_type)
  21. )
  22. @wraps(f)
  23. def wrapped(*args, **kwargs):
  24. return Tomorrow(
  25. pool.submit(f, *args, **kwargs),
  26. timeout=timeout
  27. )
  28. return wrapped
  29. return decorator
  30. def threads(n, timeout=None):
  31. return async(n, ThreadPoolExecutor, timeout)

示范代码

(虽然不如gevent方便,不如multiprocessing.dummy性能高,但是至少有了进度条功能

  1. import time
  2. import requests
  3. import sys
  4. from tomorrow import threads
  5. s=requests.Session()
  6. jishu=0
  7. @threads(10)
  8. def download(url):
  9. global jishu
  10. for _ in range(5):
  11. try:
  12. jishu+=1
  13. aa=s.get(url)
  14. sys.stderr.write(‘%s \r‘%jishu)
  15. except:
  16. pass
  17. return aa
  18. urls = [‘http://p.3.cn/prices/mgets?skuIds=J_1273600‘]*100
  19. if __name__ == "__main__":
  20. start = time.time()
  21. responses = [download(url) for url in urls]
  22. html = [response.text for response in responses]
  23. end = time.time()
  24. print ("Time: %f seconds" % (end - start))




文章来自:http://www.cnblogs.com/pyld/p/4716744.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3