进程和线程的概念
进程:进程是操作系统进行资源分配和调度的一个独立单位,是应用程序运行的载体,拥有独立的地址空间(系统资源)。作为拥有资源的基本单位。
线程:线程是程序执行中一个单一的顺序控制流程,是程序执行流的最小单元,是处理器调度和分派的基本单位。作为调度和分配的基本单位。
https://www.cnblogs.com/ChenZhongzhou/p/5685637.html
多线程的使用
import threading
import time
# t1 = threading.Thread(target = 函数名) #线程1,指针指向
# https://www.runoob.com/python3/python3-date-time.html
#线程类
#线程一,每隔一秒打印日期
class Line(threading.Thread):
def __init__(self):
super(Line, self).__init__()#继承
def run(self):#多线程threading.Thread,函数必须用run
while(1):
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))#输出时间
time.sleep(1)#延时一秒
# 线程二,每隔两秒打印姓名
class Line2(threading.Thread):
def __init__(self,name):
super(Line2, self).__init__()#继承
self.name = name
def run(self):
while(1):
#只执行四次
for i in range(4):
print(self.name)#输出名称
time.sleep(2)#延时2秒
break
T=Line()
t=Line2("张三")
#线程的调用
t.start()
T.start()
线程模块
python语言爱好者的个人空间_哔哩哔哩_bilibili
【莫烦Python】Threading 学会多线程 Python_哔哩哔哩_bilibili
- threading.currentThread(): 返回当前的线程变量。
- threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
- threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
除了使用方法外,线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:
- run(): 用以表示线程活动的方法。
- start():启动线程活动。
- join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
- isAlive(): 返回线程是否活动的。
- getName(): 返回线程名。
- setName(): 设置线程名。
线程同步
意义:同时进行两个线程,当你修改其中一个来修改共享内容时,另一个仍在执行,这是数据就是不统一的,需要先将一个进程锁定,在该进程进行完后释放,然后另一进程开始。
注:使用 Thread 对象的 Lock 和 Rlock 可以实现简单的线程同步,这两个对 象都有 acquire 方法和 release 方法,对于那些需要每次只允许一个线程操作的数据,可以将其操作放到 acquire 和 release 方法之间。
threadLock = threading.Lock()
threadLock.acquire() # 获取锁,用于线程同步
只需要一个线程操作的数据
threadLock.release() # 释放锁,开启下一个线程
#菜鸟教程的实例
import threading
import time
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):
print ("开启线程: " + self.name)
# 获取锁,用于线程同步
threadLock.acquire()
print_time(self.name, self.delay, 3)
# 释放锁,开启下一个线程
threadLock.release()
def print_time(threadName, delay, counter):
#循环三次
while counter:
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
threadLock = threading.Lock()
#锁的嵌套
threadLock = threading.RLock()
threads = []#创建一个列表存放所有的线程
# 创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# 开启新线程
thread1.start()
thread2.start()
# 添加线程到线程列表
threads.append(thread1)
threads.append(thread2)
# 等待所有线程完成
for t in threads:
t.join()#多线程的等待,相当于单线程的t.wait()
print ("退出主线程")
递归锁
嵌套锁用threading.Lock()不合理,会卡死在一个锁里,应该用 threadLock = threading.RLock()
import threading
threadLock = threading. RLock()#嵌套锁不能再用threading. Lock()
def H1():
threadLock.acquire()#获取锁2
print(1)
threadLock.release()#释放锁2
def H2():
threadLock.acquire()#获取锁1
print(2)
threadLock.release()#释放锁3
def h():
threadLock.acquire()#获取锁1
H1()
H2()
threadLock.release()#释放锁1
h()
线程优先级队列( Queue)
Queue 模块中的常用方法:
- Queue.qsize() 返回队列的大小
- Queue.empty() 如果队列为空,返回True,反之False
- Queue.full() 如果队列满了,返回True,反之False
- Queue.full 与 maxsize 大小对应
- Queue.get([block[, timeout]])获取队列,timeout等待时间
- Queue.get_nowait() 相当Queue.get(False)
- Queue.put(item) 写入队列,timeout等待时间
- Queue.put_nowait(item) 相当Queue.put(item, False)
- Queue.task_done() 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
- Queue.join() 实际上意味着等到队列为空,再执行别的操作
https://www.runoob.com/python3/python3-multithreading.html