学习笔记--线程
多任务
同时做多件事情(多个任务)就叫多任务。
并发:CPU小于当前的执行的任务。是假的多任务
并行:CPU 大于当前执行的任务。是真的多任务
实现多任务的三种方式
- 线程
- 进程
- 协程
线程
线程(thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。
import threading,time
def test1():
time.sleep(3)
print('This is test1')
def test2():
print('This is test2')
if __name__ == '__main__':
t1 = threading.Thread(target=test1)
t2 = threading.Thread(target=test2)
t1.start()
t2.start()
防护线程
用来服务于用户线程;不需要上层逻辑介入。
import threading,time
def test1():
time.sleep(3)
print('This is test1')
def test2():
print('This is test2')
if __name__ == '__main__':
t1 = threading.Thread(target=test1,daemon=True)
t2 = threading.Thread(target=test2)
t1.start()
t2.start()
join()方法
实现子线程结束完毕,主线程才继续执行。
import threading,time
def test1():
time.sleep(3)
print('This is test1')
def test2():
print('This is test2')
if __name__ == '__main__':
t1 = threading.Thread(target=test1,daemon=True)
t2 = threading.Thread(target=test2)
t1.start()
t1.join()
t2.start()
查看线程数量
使用enumerater()方法
import threading,time
def test1():
time.sleep(3)
print('This is test1')
def test2():
time.sleep(2)
print('This is test2')
if __name__ == '__main__':
t1 = threading.Thread(target=test1)
t2 = threading.Thread(target=test2)
t1.start()
t2.start()
print(threading.enumerate())
#[<_MainThread(MainThread, started 728)>, <Thread(Thread-1, started 18616)>, <Thread(Thread-2, started 2552)>]
子线程的执行与创建
当调用Thread的时候,不会创建线程。只有当调用Thread创建出来的实例对象的start方法的时候,才会创建线程以及开始运行这个线程。
继承Thread类创建线程
import threading
class My_thread(threading.Thread):
def __init__(self,name):
super().__init__(name=name)
def run(self):
print(self.name)
if __name__ == '__main__':
A=My_thread('haha')
A.start()
线程之间的通信
import threading
def test1():
global num
num = 1
num += 1
print(f'test1-{num}')
def test2():
print(f'test2-{num}')
if __name__ == '__main__':
num = 1
t1 = threading.Thread(target=test1)
t2 = threading.Thread(target=test2)
t1.start()
print()
t2.start()
线程传递参数
import threading
def test1(args):
print(f'This is test1 parameter --{args}')
if __name__ == '__main__':
t1 = threading.Thread(target=test1,args=('hello',))
t1.start()
线程的active_count与current_thread方法
active_count获取当前活跃线程数量
current_thread获取当前的线程
import threading,time
from threading import active_count,current_thread
def test1(args):
# print(f'This is test1 parameter --{args}')
time.sleep(1)
if __name__ == '__main__':
t1 = threading.Thread(target=test1,args=('hello',))
t1.start()
print(active_count())
print(current_thread().name)
多任务版UDP聊天
步骤:
1.创建套接字
2.绑定本地信息
3.获取对方IP和端口
4.发送、接收
5.创建两个线程,去执行功能
import socket
import threading
import time
def receive_data(args):
while True:
time.sleep(1)
data = args.recvfrom(1024)
print('%s:%s' %(data[1],data[0].decode('utf-8')))
def send_data(args):
while True:
time.sleep(1)
data = input('输入发送的数据:')
args.sendto(data.encode('utf-8'),('127.0.0.1',8888))
def main():
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind(('127.0.0.1', 8888))
t1 = threading.Thread(target=send_data,args=(udp_socket,))
t2 = threading.Thread(target=receive_data,args=(udp_socket,))
t1.start()
t2.start()
if __name__ == '__main__':
main()
多任务版TCP
服务端
import socket
import threading
def main():
print('等待客户端连接中')
while True:
tcp_server.listen()
new_server, addr = tcp_server.accept()
while True:
try: #由于关闭客户端recv没有数据接收会报错
data = new_server.recv(1024).decode('utf-8')
print(data)
new_server.send('收到!'.encode('utf-8'))
except Exception as e:
print(e)
break
new_server.close()
tcp_server.close()
if __name__ == '__main__':
tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server.bind(('127.0.0.1', 9876))
for i in range(3):
t = threading.Thread(target=main)
t.start()
客户端
import socket
import threading
import time
def receive_data(arg):
while True:
data = arg.recv(1024).decode('utf-8')
print(data)
def send_data(arg):
while True:
time.sleep(1)
info=input('输入发送数据:')
arg.send(info.encode('utf-8'))
def main():
tcp_client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcp_client.connect(('127.0.0.1',9876))
t1 = threading.Thread(target=receive_data,args=(tcp_client,))
t2 = threading.Thread(target=send_data,args=(tcp_client,))
t1.start()
t2.start()
if __name__ == '__main__':
main()
互斥锁与死锁
互斥锁
互斥锁是一个二元变量,其状态为开锁(允许0)和上锁(禁止1),将某个共享资源与某个特定互斥锁在逻辑上绑定(要申请该资源必须先获取锁)。
- 互斥锁的工作原理
- 访问公共资源前,必须申请该互斥锁,若处于开锁状态,则申请到锁对象,并立即占有该锁,以防止其他线程访问该资源;如果该互斥锁处于锁定状态,则阻塞当前线程。
- 只有锁定该互斥锁的进程才能释放该互斥锁,其他线程试图释放无效。
互斥锁保证了每次只有一个线程进行写入操作,从而保证了多线程情况下数据的正确性。
- 互斥锁的使用
1.创建互斥锁 mutex = threading.Lock()
2.使用锁mutex.acquire()
3.释放锁mutex.release()
def func1(arg,nums):
global num
arg.acquire()
for i in range(nums):
num += 100
# arg.release() #由于本次没有释放锁,锁得到的结果不会将func2中的进行运算
def func2(arg):
global num
arg.acquire()
num += 100
arg.release()
def main():
mutex = threading.Lock()
t1 = threading.Thread(target=func1,args=(mutex,100))
t2 = threading.Thread(target=func2,args=(mutex,))
t1.start()
t2.start()
print(num)
if __name__ == '__main__':
num=100
main()
死锁
所谓死锁是指多个线程因竞争资源而造成的一种僵局(互相等待),若无外力作用,这些线程都将无法向前推进的一种行为。
- 死锁的两种的情况
- 一般情况下,如果同一个线程先后两次调用 lock ,在第二次调用时,由于锁已经被占用,该线程会 挂起等待别的线程释放锁,然而锁正是被自己占用着的,该线程又被挂起而没有机会释放锁,因此 就永远处于挂起等待状态了(一个资源程序比较长开始使用了这把锁,中途忘记这把锁被使用,再次上锁;或者在这个资源程序中调用其他函数也使用了这把锁)
- 另一种:若 线程 A 获 得了锁 1 ,线程 B 获得了锁 2 ,这时线程 A 调用 lock 试图获得锁 2 ,结果是需要挂起等待线程 B 释放 锁 2 ,而这时线程 B 也调用 lock 试图获得锁 1 ,结果是需要挂起等待线程 A 释放锁 1 ,于是线程 A 和 B 都 永远处于挂起状态了。
线程同步
- 使用方法:
- 线程同步:cond = threading.Condtion()
- 等待:cond.wait()
- 唤醒:cond.notify()
实例1;
实现:
天猫精灵:小爱同学
小爱同学:在
天猫精灵:现在几点了?
小爱同学:你猜猜现在几点了
#1.使用互斥锁
import threading
import time
class Xiaoai(threading.Thread):
def __init__(self,lock,name='小爱同学'):
super(Xiaoai, self).__init__(name=name)
self.lock = lock
def run(self):
self.lock.acquire()
print('%s:在'%self.name)
self.lock.release()
time.sleep(1)
self.lock.acquire()
print('%s:你猜猜现在几点了' %self.name)
self.lock.release()
class Tian_cat(threading.Thread):
def __init__(self,lock,name='天猫精灵'):
super(Tian_cat, self).__init__(name=name)
self.lock = lock
def run(self):
self.lock.acquire()
print('%s:小爱同学' %self.name)
self.lock.release()
time.sleep(1)
self.lock.acquire()
print('%s:现在几点了' %self.name)
self.lock.release()
def main():
lock = threading.Lock()
t1 = Tian_cat(lock)
t2 = Xiaoai(lock)
t1.start()
t2.start()
if __name__ == '__main__':
main()
#2.使用同步线程
import threading
class Xiaoai(threading.Thread):
def __init__(self, cond, name='小爱同学'):
super(Xiaoai, self).__init__(name=name)
self.cond = cond
def run(self):
if self.cond.acquire(): #判断是否获得锁
self.cond.notify() #唤醒线程
print('%s:在' % self.name)
self.cond.wait() #等待其他线程唤醒
print('%s:你猜猜现在几点了' % self.name)
self.cond.release() # 释放资源
class Tian_cat(threading.Thread):
def __init__(self, cond,name='天猫精灵'):
super(Tian_cat, self).__init__(name=name)
self.cond = cond
def run(self):
if self.cond.acquire(): #判断是否获得锁
print('%s:小爱同学' % self.name)
self.cond.wait() #等待其他线程唤醒
print('%s:现在几点了' % self.name)
self.cond.notify() #唤醒
self.cond.release() #释放资源
def main():
cond = threading.Condition()
t1 = Tian_cat(cond)
t2 = Xiaoai(cond)
t1.start()
t2.start()
if __name__ == '__main__':
main()