Python之守护线程与锁

2023-01-31 00:01:52 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

  #郑州不  孕 不  育 医院:Http://wapyyk.39.net/zz3/zonghe/1d427.html/

  # # 模拟计算在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

  '''


相关文章