** 以下代码均在Python3.5下测试 **
1. 多线程
threading模块实现
import threading, time
class Worker(threading.Thread):
def __init__(self, num):
super(Worker,self).__init__()
self.num = num
def run(self):
print("Thread-%d start..." % self.num)
time.sleep(1)
print("Thread-%d end..." % self.num)
workers = []
n = 5
st = time.time()
for i in range(n):
workers.append(Worker(i))
for i in range(n):
workers[i].start()
for i in range(n):
workers[i].join()
et = time.time()
print("cost %.3fs" % (et-st))
map实现
from multiprocessing.dummy import Pool
from urllib import request as rq
urls = []
for i in range(100):
urls.append("http://acm.split.hdu.edu.cn/showproblem.php?pid=%d" % (1000+i))
pool = Pool(5)
pool.map(rq.urlopen, urls)
pool.close()
pool.join()
2. 多进程
import multiprocessing, time
class Worker(multiprocessing.Process):
def __init__(self, num):
super(Worker,self).__init__()
self.num = num
def run(self):
print("Process-%d start..." % self.num)
time.sleep(1)
print("Process-%d end..." % self.num)
#这句必须写
if __name__ == '__main__':
workers = []
n = 5
st = time.time()
for i in range(n):
workers.append(Worker(i))
for i in range(n):
workers[i].start()
for i in range(n):
workers[i].join()
et = time.time()
print("cost %.3fs" % (et-st))
3. 协程
gevent实现
import gevent
from gevent import monkey
from urllib import request as rq
monkey.patch_socket()
urls = []
for i in range(100):
urls.append("http://acm.split.hdu.edu.cn/showproblem.php?pid=%d" % (1000+i))
def worker(i):
rq.urlopen(urls[i])
tasks = [gevent.spawn(worker, i) for i in range(100)]
gevent.joinall(tasks)
async/await实现
import asyncio
async def worker():
await asyncio.sleep(1)
loop = asyncio.get_event_loop()
tasks = [worker() for _ in range(20)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
网友评论