36 daemon, daemon threads, locks

  1. With the end of the main daemon process execution code ends
  2. Daemon thread will wait for the end of the other sub-thread after the end of the main thread until the end
import time
from threading import Thread
def func1():
    while True:
        print('*'*10)
        time.sleep(1)
def func2():
    print('in func2')
    time.sleep(5)

t = Thread(target=func1,)
t.daemon = True
t.start()
t2 = Thread(target=func2,)
t2.start()
t2.join()
print('主线程')

The main process after the end of its code has finished running the (daemon will be recovered at this point), then the main course would have been the guardian of the child and other non-playing resource recovery processes run after the completion of the child process (or will cause zombies process), it will end.
Non daemon main thread has finished running in other multithreaded operation considered complete (daemon thread is recovered at this point). Since the end of the end of the process means that the main thread, the whole process will be recycled resources, and the process must be guaranteed before the end of the non-daemon threads have finished running.

lock

GIL lock, the lock thread.

from threading import Lock,Thread
import time

def func():
    global n
    tem = n
    time.sleep(2)
    n = tem-1
n = 10
t_list = []
for i in range(10):
    t = Thread(target=func)
    t.start()
    t_list.append(t)
for i in t_list:
    i.join()
print(n)

Output:
Here Insert Picture Description
ten thread to fetch the value of n, n to almost simultaneously take a value of 10, followed by 10 threads are taken to their value n decremented by one.

Lock

from threading import Lock,Thread
import time

def func(lock):
    global n
    lock.acquire()
    tem = n
    time.sleep(2)
    n = tem-1
    lock.release()
    print(n)
n = 10
t_list = []
lock = Lock()
for i in range(10):
    t = Thread(target=func,args=(lock,))
    t.start()
    t_list.append(t)
for i in t_list:
    i.join()

Output:
Here Insert Picture Description
Scientists noodles problem

from threading import Lock, Thread
import time

noodle_lock = Lock()	#互斥锁
fork_lock = Lock()


def eat1(name):
    noodle_lock.acquire()
    print('%s take the noodles' % name)
    fork_lock.acquire()
    print('%s take the fork' % name)
    print('%s eat the noodles' % name)
    fork_lock.release()
    noodle_lock.release()

def eat2(name):
    fork_lock.acquire()
    print('%s take the fork' % name)
    time.sleep(1)
    noodle_lock.acquire()
    print('%s take the noodles' % name)
    print('%s eat the noodles' % name)
    noodle_lock.release()
    fork_lock.release()

Thread(target=eat1,args=('zhangsan',)).start()
Thread(target=eat2,args=('lisi',)).start()
Thread(target=eat1,args=('wangwu',)).start()
Thread(target=eat2,args=('123',)).start()

Output:
Here Insert Picture Description
the output result of blockage occurs.

Recursive lock in the same thread, can be many timesrequire()

from threading import RLock,Thread   # 递归锁
import time
fork_lock = noodle_lock  = RLock()   # 一个钥匙串上的两把钥匙
def eat1(name):
    noodle_lock.acquire()
    print('%s take the noodles' % name)
    fork_lock.acquire()
    print('%s take the fork' % name)
    print('%s eat the noodles' % name)
    fork_lock.release()
    noodle_lock.release()

def eat2(name):
    fork_lock.acquire()
    print('%s take the fork' % name)
    time.sleep(1)
    noodle_lock.acquire()
    print('%s take the noodles' % name)
    print('%s eat the noodles' % name)
    noodle_lock.release()
    fork_lock.release()

Thread(target=eat1,args=('zhangsan',)).start()
Thread(target=eat2,args=('lisi',)).start()
Thread(target=eat1,args=('wangwu',)).start()
Thread(target=eat2,args=('123',)).start()

Output:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43265998/article/details/91549552