Skip to content

Instantly share code, notes, and snippets.

@raphaelsoul
Last active March 28, 2016 09:18
Show Gist options
  • Select an option

  • Save raphaelsoul/29e8526bfbba43e7309a to your computer and use it in GitHub Desktop.

Select an option

Save raphaelsoul/29e8526bfbba43e7309a to your computer and use it in GitHub Desktop.
对比测试多线程性能
from threading import Thread
import queue
import time
# q是任务队列
#NUM是并发线程总数
#JOBS是有多少任务
q = queue.Queue()
NUM = 2
JOBS = 10
#具体的处理函数,负责处理单个任务
def do_somthing_using(arguments):
print(arguments)
#这个是工作进程,负责不断从队列取数据并处理
def working():
while True:
arguments = q.get()
do_somthing_using(arguments)
time.sleep(1)
q.task_done()
#fork NUM个线程等待队列
for i in range(NUM):
t = Thread(target=working)
t.setDaemon(True)
t.start()
#把JOBS排入队列
for i in range(JOBS):
q.put(i)
#等待所有JOBS完成
q.join()
import time
import threading
import requests
class CalThread(threading.Thread):
"""docstring for CalThread"""
def __init__(self,queue):
self._queue = queue
threading.Thread.__init__(self)
def run(self):
html = requests.get("https://www.baidu.com").content
with open(self.name+".html","wb") as f:
f.write(html)
#print("Thread %s return %s"%(self.name,result))
def main(runtimes=1000):
q = queue.Queue(0)
for i in range(runtimes):
t = CalThread(q)
t.start()
def main_no_thread(runtimes=1000):
for i in range(runtimes):
html = requests.get("https://www.baidu.com").content
with open(str(i)+".html","wb") as f:
f.write(html)
if __name__ == '__main__':
rt = 100
t1 = time.time()
main(rt)
print("threading Total uses time: %s" % (time.time()-t1))
t2 = time.time()
main_no_thread(rt)
print("Total uses time: %s" % (time.time()-t2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment