首页 > 代码库 > 多线程小例子

多线程小例子

multiprocessing 方式

  1. #!/usr/bin/env python
  2. # Version = 3.5.2
  3. # __auth__ = ‘无名小妖‘
  4. from multiprocessing.dummy import Pool as ThreadPool
  5. import time
  6. import urllib.request
  7. urls = [‘https://www.python.org/‘,
  8. ‘https://www.yahoo.com/‘,
  9. ‘https://github.com/‘ ]
  10. start = time.time()
  11. def open_url(url):
  12. print(‘GET {}‘.format(url))
  13. response = urllib.request.urlopen(url)
  14. results = response.read()
  15. print(‘{} bytes received.‘.format(len(results)))
  16. for url in urls:
  17. open_url(url)
  18. print(‘Normal:‘, time.time() - start)
  19. start2 = time.time()
  20. # 开8个 worker,没有参数时默认是 cpu 的核心数
  21. pool = ThreadPool(processes=8)
  22. # 在线程中执行 urllib2.urlopen(url) 并返回执行结果
  23. pool.map(open_url,urls)
  24. print(‘Thread Pool:‘, time.time() - start2)
  25. pool.close()
  26. pool.join()

gevent 方式

  1. #!/usr/bin/env python
  2. # Version = 3.5.2
  3. # __auth__ = ‘无名小妖‘
  4. from urllib import request
  5. import gevent, time
  6. from gevent import monkey
  7. monkey.patch_all() # 把当前程序的所有的io操作给我单独的做上标记
  8. def f(url):
  9. print(‘GET: %s‘ % url)
  10. resp = request.urlopen(url)
  11. data = resp.read()
  12. print(‘%d bytes received from %s.‘ % (len(data), url))
  13. urls = [‘https://www.python.org/‘,
  14. ‘https://www.yahoo.com/‘,
  15. ‘https://github.com/‘]
  16. time_start = time.time()
  17. for url in urls:
  18. f(url)
  19. print("同步cost", time.time() - time_start)
  20. async_time_start = time.time()
  21. gevent.joinall([
  22. gevent.spawn(f, ‘https://www.python.org/‘),
  23. gevent.spawn(f, ‘https://www.yahoo.com/‘),
  24. gevent.spawn(f, ‘https://github.com/‘),
  25. ])
  26. print("异步cost", time.time() - async_time_start)



null


多线程小例子