首页 > 代码库 > 多线程小例子
多线程小例子
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)
null
多线程小例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。