多线程小例子
Posted 无名小妖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程小例子相关的知识,希望对你有一定的参考价值。
multiprocessing 方式
#!/usr/bin/env python
# Version = 3.5.2
# __auth__ = ‘无名小妖‘
from multiprocessing.dummy import Pool as ThreadPool
import time
import urllib.request
urls = [‘https://www.python.org/‘,
‘https://www.yahoo.com/‘,
‘https://github.com/‘ ]
start = time.time()
def open_url(url):
print(‘GET {}‘.format(url))
response = urllib.request.urlopen(url)
results = response.read()
print(‘{} bytes received.‘.format(len(results)))
for url in urls:
open_url(url)
print(‘Normal:‘, time.time() - start)
start2 = time.time()
# 开8个 worker,没有参数时默认是 cpu 的核心数
pool = ThreadPool(processes=8)
# 在线程中执行 urllib2.urlopen(url) 并返回执行结果
pool.map(open_url,urls)
print(‘Thread Pool:‘, time.time() - start2)
pool.close()
pool.join()
gevent 方式
#!/usr/bin/env python
# Version = 3.5.2
# __auth__ = ‘无名小妖‘
from urllib import request
import gevent, time
from gevent import monkey
monkey.patch_all() # 把当前程序的所有的io操作给我单独的做上标记
def f(url):
print(‘GET: %s‘ % url)
resp = request.urlopen(url)
data = resp.read()
print(‘%d bytes received from %s.‘ % (len(data), url))
urls = [‘https://www.python.org/‘,
‘https://www.yahoo.com/‘,
‘https://github.com/‘]
time_start = time.time()
for url in urls:
f(url)
print("同步cost", time.time() - time_start)
async_time_start = time.time()
gevent.joinall([
gevent.spawn(f, ‘https://www.python.org/‘),
gevent.spawn(f, ‘https://www.yahoo.com/‘),
gevent.spawn(f, ‘https://github.com/‘),
])
print("异步cost", time.time() - async_time_start)
以上是关于多线程小例子的主要内容,如果未能解决你的问题,请参考以下文章