python线程基础学习
1. 线程通信‘
2. 线程通信强化
3. condition线程通信与事件
4. 线程调度
5. 生产者消费者模式
6. 线程池
7. 定时线程
8. with用法
9. 前台线程
10. TLS
++++++++++++++++++++++++++++++++++++++++++++++
1.线程通信:
用set来激发事件以达到通知效果
import threading
import time
def goevent():
e=threading.Event() #事件
def go():
e.wait() #等待,后续代码不执行,等待set再执行
e.clear() #清理
print("go")
threading.Thread(target=go).start() #开启一个线程
return e
t=goevent()
time.sleep(5) #5s后才激发事件打印go
t.set() #激发事件
2.线程通信强化(传参):
import threading
import time
def goevent():
e=threading.Event() #事件
def go():
for i in range(10):
e.wait() #等待
e.clear() #重置
print(i,"go")
threading.Thread(target=go).start() #开启一个线程
return e
t=goevent()
for i in range(10):
time.sleep(i) # i s后才激发事件打印go
t.set() #通知
3.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()
def go