---恢复内容开始---
1.线程(thread)
所有的指令都是有CPU控制的,执行的,运算的。
An executing instance of a program is called a process.
Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, (环境变量)environment variables, a priority class(优先级), minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.
1 import threading 2 import time 3 def run(n): 4 print("task", n) 5 time.sleep(2) 6 7 t1 = threading.Thread(target=run,args=("t1",)) 8 t2 = threading.Thread(target=run,args=("t2",)) 9 10 t1.start() 11 t2.start() 12 13 #run("t1") #t1.join() 等待t1 进程完成 14 #run("t2")
5. 多线程案例
1 import threading 2 3 class MyThread(threading.Thread): 4 def __init__(self,n): 5 super(MyThread,self).__init__() 6 self.n = n 7 8 def run(self): 9 print("runnint task", self.n) 10 11 12 t1 = MyThread("t1") 13 t2 = MyThread("t2") 14 15 t1.start() 16 t2.start()
6. 主线程与子线程
1 import threading 2 import time 3 def run(n): 4 print("task", n, threading.current_thread(), threading.active_count()) #查看当前线程是子线程还是主线程 5 time.sleep(2) 6 7 start_time = time.time() 8 t_objs = [] #列表,存线程实例 9 for i in range(50): 10 t = threading.Thread(target=run,args=("t-%s" %i ,)) #注意逗号不能丢 11 t.start() 12 t_objs.append(t) 13 14 for t in t_objs: 15 t.join() #等待所有的子进程结束 16 17 print("----------all threads has finished..", threading.current_thread(), threading.active_count())#活动线程的个数 18 print("cost:", time.time()-start_time) #计算整个进程的时间
7. 守护线程
不管子线程是否结束,主线程运行完程序就退出了
import threading import time def run(n): print("task", n, threading.current_thread(), threading.active_count()) #查看当前线程是子线程还是主线程 time.sleep(2) start_time = time.time() t_objs = [] #列表,存线程实例 for i in range(50): t = threading.Thread(target=run,args=("t-%s" %i ,)) #注意逗号不能丢 t.setDaemon(True) #把当前线程设置为守护线程,(相当于仆人了,不管了) t.start() t_objs.append(t) for t in t_objs: t.join() #等待所有的子进程结束 print("----------all threads has finished..", threading.current_thread(), threading.active_count())#活动线程的个数 print("cost:", time.time()-start_time) #计算整个进程的时间
8. 线程锁(也称互斥锁Mutex)之 GIL vs Lock
图片来源:https://www.cnblogs.com/alex3714/articles/5230609.html
8.1 递归锁
RLock 与 Lock
9. 线程锁之信号量(Semaphore)
10.线程之 Events (事件)
红绿灯的实例: 设置全局变量控制红绿灯色切换。
标志位的设定
方法:event.set()
event.clear()
event.wait()
event.is_set()
11.队列(queue)
注:python2.x中为import Queue; python3.x 中都为小写queue。
①定义:一个有顺序的容器,
②作用:a.解耦:
b.提高运行效率;
③队列与列表:
都有顺序;列表取出一个数据相当于复制一份,原列表中依然存在着个数据;对列中拿走一个数据,就从队列中消失了。
④ 方法:
queue.Queue() 生成一个队列;先进后出
queue.Queue(maxsize=num) 生成一个队列,并限制大小
put("xxx") 往队列中放数据;
put(3) 放入整数;
qsize() 查看队列大小;
get() 取队列中的数据,(无参数,因为先入先出),最后一个取出来,再取会卡住,
可用get_nowait(),会抛出异常;
or get(block = False) 同上,也会抛出异常;
or get(timeout = 1) ,卡一秒后抛出异常;
queue.LifoQueue() #last in first out #后进先出
queue.PriorityQueue #存储数据是可设置优先级的队列,(VIP数据)
task_done()
12. 生产者消费者模型
1 import queue 2 import threading 3 import time 4 5 q = queue.Queue(maxsize=10) 6 7 def Producer (name): 8 count = 1 9 while True: 10 q.put("骨头%s" %count) 11 print("生产了骨头" ,count) 12 count += 1 13 time.sleep(0.3) 14 15 16 def Consumer(name): 17 #while q.qsize() > 0: 18 while True: 19 print("[%s] 取到[%s] 并吃了它。。。" %(name, q.get())) 20 time.sleep(1) 21 22 p = threading.Thread(target=Producer, args=("xiaolaizi",)) 23 c = threading.Thread(target=Consumer, args=("大黄狗",)) 24 c1 = threading.Thread(target=Consumer, args=("小奶狗",)) 25 p.start() 26 c.start() 27 c1.start()
---恢复内容结束---