最近用到了多線程的調(diào)用,看前輩們的自動(dòng)化用例實(shí)在是看不懂辟拷。所以網(wǎng)上搜了些資料學(xué)習(xí)缴阎,整理如下蝙斜。
首先,需要知道要import哪些包
python2有3個(gè)可用:
????????????????import thread
????????????????import threading
????????????????import multiprocessing(進(jìn)程)
python3新增了一個(gè)concurrent.futures泌绣,也是超好用的
其次钮追,舉個(gè)小例子演示怎么使用
場(chǎng)景:有個(gè)打印當(dāng)前時(shí)間的函數(shù)print_time,想要并發(fā)執(zhí)行,
1阿迈、import thread
調(diào)用方式:thread.start_new_thread(print_time, ("Thread-1", 2,))元媚,拷貝下方代碼,可以直接運(yùn)行
#-*- coding: utf-8 -*-
import thread
import time
# 為線程定義一個(gè)函數(shù)
def print_time(threadName, delay):
count =0
? ? while count <5:
time.sleep(delay)
count +=1
? ? ? ? print "%s: %s" % (threadName, time.ctime(time.time()))
if __name__ =='__main__':
# 創(chuàng)建兩個(gè)線程-----------使用thread
? ? try:
thread.start_new_thread(print_time, ("Thread-1", 2,))
thread.start_new_thread(print_time, ("Thread-2", 4,))
except:
print "Error: unable to start thread"
? ? while 1:
pass
2苗沧、import threading
調(diào)用方法1:直接使用threading.Thread調(diào)用刊棕;方法2:改寫threading.Thread的執(zhí)行方法
方法1:
#-*- coding: utf-8 -*-
# import thread
import time
import threading
exitFlag =0
def print_time(threadName, delay):
count =0
? ? while count <5:
if exitFlag:
(threading.thread).exit()
time.sleep(delay)
count +=1
? ? ? ? print "%s: %s" % (threadName, time.ctime(time.time()))
if __name__ =='__main__':
threads = []
thread1 = threading.Thread(target=print_time,args = ("thread-1",2))
thread2 = threading.Thread(target=print_time, args=("thread-2", 2))
threads.append(thread1)
threads.append(thread2)
for threadin threads:
thread.start()
for threadin threads:
thread.join()
方法2:
#-*- coding: utf-8 -*-
# import thread
import time
import threading
exitFlag =0
class myThread(threading.Thread):
def __init__(self,threadID,name,delay):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.delay = delay
def run(self):#把要執(zhí)行的代碼寫到run函數(shù)里面 線程在創(chuàng)建后會(huì)直接運(yùn)行run函數(shù)
? ? ? ? print "Starting "+self.name
print_time(self.name,self.delay)
print "Exiting "+self.name
def print_time(threadName, delay):
count =0
? ? while count <5:
if exitFlag:
# threading.thread.exit()
? ? ? ? ? ? (threading.thread).exit()
time.sleep(delay)
count +=1
? ? ? ? print "%s: %s" % (threadName, time.ctime(time.time()))
if __name__ =='__main__':
# 使用threading.thread------------------
? ? thread1 = myThread(1,"thread-1",1)
thread2 = myThread(2,"thread-2",2)
thread1.start()
thread2.start()
3、import?multiprocessing
#-*- coding: utf-8 -*-
import time
import multiprocessing
exitFlag =0
def print_time(threadName, delay):
count =0
? ? while count <5:
if exitFlag:
# threading.thread.exit()
? ? ? ? ? ? (threading.thread).exit()
time.sleep(delay)
count +=1
? ? ? ? print "%s: %s" % (threadName, time.ctime(time.time()))
if __name__ =='__main__':
# 使用multiprocessing---------------
? ? threads = []
thread1 = multiprocessing.Process(target=print_time,args = ("thread-1",2))
thread2 = multiprocessing.Process(target=print_time, args=("thread-2", 2))
threads.append(thread1)
threads.append(thread2)
for threadin threads:
thread.start()
for threadin threads:
thread.join()
總結(jié)
進(jìn)程和線程怎么選取待逞,菜鳥教程上有個(gè)帖子寫的特別好:CPU密集型選擇多進(jìn)程甥角;IO密集型選擇多線程。原文如下:
資料顯示识樱,如果多線程的進(jìn)程是CPU密集型的嗤无,那多線程并不能有多少效率上的提升,相反還可能會(huì)因?yàn)榫€程的頻繁切換怜庸,導(dǎo)致效率下降当犯,推薦使用多進(jìn)程;如果是IO密集型割疾,多線程進(jìn)程可以利用IO阻塞等待時(shí)的空閑時(shí)間執(zhí)行其他線程嚎卫,提升效率。(引自:https://www.runoob.com/w3cnote/python-single-thread-multi-thread-and-multi-process.html)