zoukankan      html  css  js  c++  java
  • 多线程小例子

    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)





  • 相关阅读:
    分布式id生成
    DB主从一致性架构优化4种方法
    Mysql在大型网站的应用架构演变
    win10下iis绑定局域网ip无效的解决方案
    css随笔
    html标签说明
    C# JSON序列化日期格式问题
    使用事件机制相比直接调用函数的优势
    C#绑定事件时使用匿名函数
    C# t4模版引擎笔记 引入外部dll
  • 原文地址:https://www.cnblogs.com/wumingxiaoyao/p/7047853.html
Copyright © 2011-2022 走看看