线程之间通信强化
import threading
import time
def goevent():
e = threading.Event() # 事件
def go():
for i in range(10):
e.wait() # 等待事件,线程卡顿,等待set消息,只调用一次
e.clear() # 重置线程等待
print("go",i)
threading.Thread(target=go).start() # 创建一个线程
return e
t = goevent()
for i in range(5):
time.sleep(i)
t.set()
condition 线程通信与事件
import threading
import time
def go1():
with cond:
for i in range(10):
time.sleep(1)
print(threading.current_thread().name, i)
if i == 5:
cond.wait() # 等待,只有在其他相同线程条件变量唤醒时才继续执行
print("hahahha")
'''
wait()
此方法释放底层的锁,然后阻塞,直到它
通过notify()或notify_all()调用唤醒相同的条件
在另一个线程中变量,或直到发生可选的超时。一旦
唤醒或超时,它重新获得锁定并返回。
'''
def go2():
with cond: # 使用条件变量
for i in range(10):
time.sleep(1)
print(threading.current_thread().name, i)
cond.notify() # 通知唤醒其他线程
'''
notify()
在这种情况下唤醒一个或多个线程,如果有的话。
如果调用线程没有获得这个方法的锁
线程调度
逻辑:
首先明确wait()调用后下面的程序是不会运行的,
首先线程1线绑定cond,打印出0后,线程1进入等待(注意此时线程2并没有绑定),线程2绑定cond,打印出1后
notify给线程1唤醒wait(),(此时才打印出"haha"),同时线程2的wait激活进入等待,同时1打印出2,并唤醒线程2如此循环
import threading
import time
def go1():
with cond:
for i in range(0, 10, 2):
time.sleep(1)
print(threading.current_thread().name, i)
cond.wait()
# print("hahah")
cond.notify()
def go2():
with cond:
for i in range(1, 10, 2):
time.sleep(1)
print(threading.current_thread().name, i)
cond.notify()
cond.wait()
cond = threading.Condition() # 线程条件变量
threading.Thread(target=go1).start()
threading.Thread(target=go2).start()
在队列中,有多个生产者将商品放入队列,同时又多个消费者在队列的另一头获取商品,一般
情况下会产生线程冲突,但是python已经将此处理好了,也就是说在将商品放入队列的过程中无需考虑线程冲突
# import queue
# import time
# import random
#
#
# # 生产
# class CreatorThread(threading.Thread):
# def __init__(self, index, myqueue):
# threading.Thread.__init__(self)
# self.index = index # 索引
# self.myqueue = myqueue # 队列
#
# def run(self):
# while True:
# time.sleep(3)
# num = random.randint(1, 1000000)
# self.myqueue.put("in put 生产者" + str(self.index) + str(num))
# print("in put 生产者", str(self.index), str(num))
# # self.myqueue.task_done() # 完成任务
线程池
import threadpool # 需要安装
import time
def show(name):
print('hello', name)
namelist = ["A", "B", "C", "D"]
start = time.time()
pool = threadpool.ThreadPool(7) # 线程池最大数,貌似还要远大于列表长度
requests = threadpool.makeRequests(show, namelist) # 设置参数,函数,参数列表
print(requests)
print()
for req in requests:
pool.putRequest(req) # 压入线程池开始执行
end = time.time()
print(end - start)
import time
import threading
import os
'''
def show():
os.system("say hello")
mythreading = threading.Timer(3, show).start() # 延时3秒启动show函数,只启动一次
num = 0
while True:
print('第', num, "秒")
time.sleep(1)
num += 1
'''
def show():
while True:
time.sleep(3)
print("say hello")
mythreading = threading.Timer(3, show).start() # 延时3秒启动show函数,只启动一次,不阻塞主线程
num = 0
while True:
print('第', num, "秒")
time.sleep(1)
num += 1
# mythreading.cancel() # 因为cancel取消了线程的执行,所以fun()函数不会被执行
with 作用自动打开和释放,
import threading
num = 0 # 全局变量可以在线程之间传递
mutex = threading.Lock() # 创建一个锁,threading.Lock()是一个类
class Myhtread(threading.Thread):
def run(self):
global num
with mutex:
for i in range(1000000): # 数字小的时候还是不会产生线程冲突的
num += 1
print(num)
mythread = []
for i in range(5):
t = Myhtread()
t.start()
mythread.append(t)
for thread in mythread:
thread.join() # 或者直接将thread.join()加入for i in range(5),也能解决线程冲突,但是貌似就变成单线程了
print("game over")
线程独立
import threading
import time
data = threading.local() # 每个线程私有独立储存空间
t1 = lambda x: x + 1
t2 = lambda x: x + "1"
def printdata(func, x):
data.x = x # data = threading.local() 本质上是一个类,此处为动态绑定
print(id(data.x)) # 不同地址互相独立data.x 在每个线程中是独立的
for i in range(5):
data.x = func(data.x)
print(threading.current_thread().name, data.x)
threading.Thread(target=printdata, args=(t1, 1)).start()
threading.Thread(target=printdata, args=(t2, "1")).start()
import threading
import time
# import win32api # 引用系统函数
class Mythread(threading.Thread): # 继承threading.Thread
def run(self): # run重写,
# win32api.MessageBox(0, "你的账户很危险", "来自支付宝的问候", 6)
print('hahah')
mythread = [] # 集合list
for i in range(5):
t = Mythread() # 初始化
t.setDaemon(True) #后台线程,主线程不等后台线程
t.start()
mythread.append(t) # 加入线程集合
# threading.Thread默认是前台进程,主线程必须等前台。
print("game over")