python多任务以及Thread使用
多任务——操作系统可以同时运行多个任务
import threading
import time
def test():
print("这是多线程1")
time.sleep(1)
def test2():
print("这是多线程2")
time.sleep(1)
if __name__ == "__main__":
for i in range(5):
t1=threading.Thread(target=test)
t2 = threading.Thread(target=test2)
t1.start()
t2.start()
运行结果如下:
这是多线程1
这是多线程2
这是多线程1
这是多线程2
这是多线程1
这是多线程2
这是多线程1
这是多线程2
这是多线程1
这是多线程2
线程介绍
import threading
from time import sleep
def sing():
for i in range(3):
print("正在唱歌...%d"%i)
sleep(1)
def dance():
for i in range(3):
print("正在跳舞...%d"%i)
sleep(1)
if __name__ == '__main__':
# 创建线程1
# "target" : 线程执行的目标函数
my_thread1 = threading.Thread(target=sing)
my_thread2 = threading.Thread(target=dance)
# 开启线程执行任务
my_thread1.start()
my_thread2.start()
线程运行时没有先后顺序的,主线程要等所有子线程执行完才结束
threading模块中的enumerate( )方法可以查看当前运行起来的线程的列表
import threading
# 自定义线程
class MyThread(threading.Thread):
def __init__(self, num):
# 注意: 需要调用父类的构造方法
super(MyThread, self).__init__()
self.num = num
# 任务1
def work1(self):
for i in range(self.num):
print("我是在自定义线程里面执行的", self.name)
# 重写run方法
def run(self):
self.work1()
# 创建线程
mythread = MyThread(5)
mythread.start() #start()自动调run方法
运行结果如下:
我是在自定义线程里面执行的 Thread-1
我是在自定义线程里面执行的 Thread-1
我是在自定义线程里面执行的 Thread-1
我是在自定义线程里面执行的 Thread-1
我是在自定义线程里面执行的 Thread-1
join() 方法用来阻塞主线程,等待子线程执行完成。举个例子,主线程A创建了子线程B,并使用了 join() 方法,主线程A在 join() 处就被阻塞了,等待子线程B完成后,主线程A才能执行 print('END')。如果没有使用 join() 方法,主线程A创建子线程B后,不会等待子线程B,直接执行 print('END'),如下:
import threading
import time
class thread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name='线程' + threadname)
def run(self):
time.sleep(1)
print('%s:Now timestamp is %s'%(self.name,time.time()))
threads = []
for a in range(int(5)): # 线程个数
threads.append(thread(str(a)))
for t in threads: # 开启线程
t.start()
# for t in threads: # 阻塞线程
# t.join()
print('END')
输出:
#END
#线程0:Now timestamp is 1557386321.376941
#线程3:Now timestamp is 1557386321.377937
#线程1:Now timestamp is 1557386321.377937
#线程2:Now timestamp is 1557386321.377937
#线程4:Now timestamp is 1557386321.377937
二、线程之间的通信
同步概念
同步就是协同步调,按预定的先后次序进行运行,同步是使用互斥锁完成的。
#创建锁
mutex=threading.Lock()
#锁定
mutex.acquire()
#释放
mutex.release()
二、threading.Condition()
threading.Condition() 可以理解为更加高级的锁,比 Lock 和 Rlock 的用法更高级,能处理一些复杂的线程同步问题。threading.Condition() 创建一把资源锁(默认是Rlock),提供 acquire() 和 release() 方法,用法和 Rlock 一致。此外 Condition 还提供 wait()、Notify() 和 NotifyAll() 方法。
wait():线程挂起,直到收到一个 Notify() 通知或者超时(可选参数),wait() 必须在线程得到 Rlock 后才能使用。
Notify() :在线程挂起的时候,发送一个通知,让 wait() 等待线程继续运行,Notify() 也必须在线程得到 Rlock 后才能使用。 Notify(n=1),最多唤醒 n 个线程。
NotifyAll() :在线程挂起的时候,发送通知,让所有 wait() 阻塞的线程都继续运行。
举例说明下 Condition() 使用
import threading,time
def TestA():
cond.acquire()
print('李白:看见一个敌人,请求支援')
cond.wait()
print('李白:好的')
cond.notify()
cond.release()
def TestB():
time.sleep(2)
cond.acquire()
print('亚瑟:等我...')
cond.notify()
cond.wait()
print('亚瑟:我到了,发起冲锋...')
if __name__=='__main__':
cond = threading.Condition()
testA = threading.Thread(target=TestA)
testB = threading.Thread(target=TestB)
testA.start()
testB.start()
testA.join()
testB.join()
输出
#李白:看见一个敌人,请求支援
#亚瑟:等我...
#李白:好的
#亚瑟:我到了,发起冲锋...
三、threading.Event()
threading.Event() 原理是在线程中立了一个 Flag ,默认值是 False ,当一个或多个线程遇到 event.wait() 方法时阻塞,直到 Flag 值 变为 True 。threading.Event() 通常用来实现线程之间的通信,使一个线程等待其他线程的通知 ,把 Event 传递到线程对象中。
event.wait() :阻塞线程,直到 Flag 值变为 True
event.set() :设置 Flag 值为 True
event.clear() :修改 Flag 值为 False
event.isSet() : 仅当 Flag 值为 True 时返回
下面这个例子,主线程启动子线程后 sleap 2秒,子线程因为 event.wait() 被阻塞。当主线程醒来后执行 event.set() ,子线程才继续运行,两者输出时间差 2s。
import threading
import datetime,time
class thread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name='线程' + threadname)
self.threadname = int(threadname)
def run(self):
event.wait()
print('子线程运行时间:%s'%datetime.datetime.now())
if __name__ == '__main__':
event = threading.Event()
t1 = thread('0')
#启动子线程
t1.start()
print('主线程运行时间:%s'%datetime.datetime.now())
time.sleep(2)
# Flag设置成True
event.set()
t1.join()
输出
#主线程运行时间:2019-05-30 15:51:49.690872
#子线程运行时间:2019-05-30 15:51:51.691523
threading其他常用方法:
threading.active_count():返回当前存活的线程对象的数量
threading.current_thread():返回当前线程对象
threading.enumerate():返回当前所有线程对象的列表
threading.get_ident():返回线程pid
threading.main_thread():返回主线程对象
浙公网安备 33010602011771号