Python之守护线程与锁

# 守护线程随着子线程结束而结束,与守护进程不一样--守护进程随着主进程代码执行完毕而结束
# from threading import Thread
# import time
#
# def func1():
#     while True:
#         time.sleep(1)
#         print("in func1")
# def func2():
#     print('func2 start...')
#     time.sleep(5)
#     print('func2 end...')
# if __name__ == '__main__':
#     t1 = Thread(target=func1)
#     t1.daemon = True
#     t1.start()
#     t2 = Thread(target=func2)
#     t2.start()
#     print('主线程代码完毕')
'''
输出结果:
func2 start...
主线程代码完毕
in func1
in func1
in func1
in func1
func2 end...
'''

# 多线程之数据安全问题
# from threading import Thread
# import time
#
# # 模拟计算在CPU中发生的过程,且时间放大
# def func():
#     global n
#     tmp = n
#     time.sleep(1)
#     n = tmp - 1
# if __name__ == '__main__':
#     n = 10
#     t_lst = []
#     for i in range(10):
#         t = Thread(target=func)
#         t.start()
#         t_lst.append(t)
#     for t in t_lst:t.join()
#     print(n) # 9
# 10个线程对同一个数据执行减1操作,得到的结果却是9,发生了数据安全,因此要对数据加锁
# from threading import Thread
# from threading import Lock
# import time
#
# # 模拟计算在CPU中发生的过程,且时间放大
# def func(lock):
#     global n
#     # 请求锁
#     lock.acquire()
#     tmp = n
#     time.sleep(1)
#     n = tmp - 1
#     # 释放锁
#     lock.release()
# if __name__ == '__main__':
#     n = 10
#     # 实例化一个锁
#     lock = Lock()
#     t_lst = []
#     for i in range(10):
#         t = Thread(target=func,args=(lock,))
#         t.start()
#         t_lst.append(t)
#     for t in t_lst:t.join()
#     print(n) # 0 耗时10秒,相当于同步执行了

# 当调用多个锁的时候容易造程死锁问题
# from threading import Thread
# from threading import Lock
# import time
# # 假设一个操作需要使用两个锁,func1先调用lock1
# def func1(name,lock1,lock2):
#     lock1.acquire()
#     print('%s拿到lock1'%(name))
#     time.sleep(1)
#     lock2.acquire()
#     print('%s拿到lock2'%(name))
#     time.sleep(1)
#     print('%s执行被锁的代码'%(name))
#     lock2.release()
#     print('%s释放lock2'%(name))
#     time.sleep(1)
#     lock1.release()
#     print('%s释放lock1'%(name))
# # func2先调用lock2
# def func2(name,lock1,lock2):
#     lock2.acquire()
#     print('%s拿到lock2'%(name))
#     time.sleep(1)
#     lock1.acquire()
#     print('%s拿到lock1'%(name))
#     time.sleep(1)
#     print('%s执行被锁的代码'%(name))
#     lock1.release()
#     print('%s释放lock1'%(name))
#     time.sleep(1)
#     lock2.release()
#     print('%s释放lock2'%(name))
# if __name__ == '__main__':
#     lock1 = Lock()
#     lock2 = Lock()
#     t1 = Thread(target=func1,args=('t1',lock1,lock2))
#     t1.start()
#     t2 = Thread(target=func2,args=('t2',lock1,lock2))
#     t2.start()
'''
输出结果:
t1拿到lock1
t2拿到lock2
程序卡死--发生死锁
'''
# 针对死锁情况,可以使用递归锁解决
# from threading import Thread
# from threading import RLock
# import time
# # 假设一个操作需要使用两个锁,func1先调用lock1
# def func1(name,lock1,lock2):
#     lock1.acquire()
#     print('%s拿到lock1'%(name))
#     time.sleep(1)
#     lock2.acquire()
#     print('%s拿到lock2'%(name))
#     time.sleep(1)
#     print('%s执行被锁的代码'%(name))
#     lock2.release()
#     print('%s释放lock2'%(name))
#     time.sleep(1)
#     lock1.release()
#     print('%s释放lock1'%(name))
# # func2先调用lock2
# def func2(name,lock1,lock2):
#     lock2.acquire()
#     print('%s拿到lock2'%(name))
#     time.sleep(1)
#     lock1.acquire()
#     print('%s拿到lock1'%(name))
#     time.sleep(1)
#     print('%s执行被锁的代码'%(name))
#     lock1.release()
#     print('%s释放lock1'%(name))
#     time.sleep(1)
#     lock2.release()
#     print('%s释放lock2'%(name))
# if __name__ == '__main__':
#     lock2 = lock1 = RLock()
#     t1 = Thread(target=func1,args=('t1',lock1,lock2))
#     t1.start()
#     t2 = Thread(target=func2,args=('t2',lock1,lock2))
#     t2.start()
'''
递归锁其实是一个锁,但是可以被多处acquire
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值