python多线程怎么使用
最近用到了多线程的调用,看前辈们的自动化用例实在是看不懂。所以网上搜了些资料学习,整理如下。
首先,需要知道要import哪些包
python2有3个可用:
import thread
import threading
import multiprocessing(进程)
python3新增了一个concurrent.futures,也是超好用的
其次,举个小例子演示怎么使用
场景:有个打印当前时间的函数print_time,想要并发执行,
1、import thread
调用方式:thread.start_new_thread(print_time, ("Thread-1", 2,)),拷贝下方代码,可以直接运行
#-*- coding: utf-8 -*-
import thread
import time
# 为线程定义一个函数
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__':
# 创建两个线程-----------使用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
调用方法1:直接使用threading.Thread调用;方法2:改写threading.Thread的执行方法
方法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):#把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
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()
总结
进程和线程怎么选取,菜鸟教程上有个帖子写的特别好:CPU密集型选择多进程;IO密集型选择多线程。原文如下:
资料显示,如果多线程的进程是CPU密集型的,那多线程并不能有多少效率上的提升,相反还可能会因为线程的频繁切换,导致效率下降,推荐使用多进程;如果是IO密集型,多线程进程可以利用IO阻塞等待时的空闲时间执行其他线程,提升效率。(引自:https://www.runoob.com/w3cnote/python-single-thread-multi-thread-and-multi-process.html)