Synchronous and asynchronous:
Synchronization: when a process executes a request, if it takes a while for the request to return information, the process will wait until it receives the return information.
Asynchrony: the process does not need to wait, but continues to perform the following operations, regardless of the state of other processes. When a message is returned, the system will inform the process to process, which can improve the efficiency of execution.
Synchronous is blocking mode, asynchronous is non blocking mode.
My understanding: synchronization refers to the connection between two things, one of which is waiting for the other. Asynchrony means that two things have nothing to do with each other.
Mutex:
Unlocked:
Code demonstration:
import threading from threading import Thread num = 0 def work1(n): global num for i in range(n): num +=1 print("in work1:",num) def work2(n): global num for i in range(n): num += 1 print("in work2:",num) def main(): t1 = Thread(target=work1,args=(1000000,)) t2 = Thread(target=work2,args=(1000000,)) t1.start() t2.start() t2.join() if __name__ == '__main__': main() print("main:",num)
Operation result:
Lock:
Code demonstration:
import threading from threading import Thread num = 0 lock = threading.Lock() #Create a lock def work1(n): global num lock.acquire() #Lock up for i in range(n): num +=1 lock.release() #Unlock print("in work1:",num) def work2(n): global num lock.acquire() #Lock up for i in range(n): num += 1 lock.release() #Unlock print("in work2:",num) def main(): t1 = Thread(target=work1,args=(1000000,)) t2 = Thread(target=work2,args=(1000000,)) t1.start() t2.start() t2.join() if __name__ == '__main__': main() print("main:",num)
Operation result:
Lock benefits: ensure that a critical piece of code can only be executed by one thread from the beginning to the end.
Disadvantages of lock: it prevents concurrent execution of multiple threads. In fact, some code containing lock can only be executed in single thread mode, which greatly reduces the efficiency. Because there can be multiple locks, different threads hold different locks, and try to acquire the lock held by the other party, it may cause deadlock.
Deadlock:
Deadlock is the situation of waiting for the other party to release the lock
The result of deadlock will cause the program to stop responding, and other tasks can no longer be processed
Example of deadlock
Code demonstration:
from threading import Lock from threading import Thread lock1 = Lock() lock2 = Lock() def work1(num): lock1.acquire() #lock1 lock up print("in work1") lock2.acquire() #lock2 lock up print("work1----") lock2.release() #lock2 unlock lock1.release() #lock1 unlock def work2(num): lock2.acquire() #lock2 lock print("in work2") lock1.acquire() #lock2 lock print("work2-----") lock1.release() #lock1 unlock lock2.release() #lock2 unlock if __name__ == '__main__': t1 = Thread(target=work1,args=(1000000,)) t2 = Thread(target=work2,args=(1000000,)) t1.start() t2.start()
Operation result:
Avoid deadlock
Code demonstration:
from threading import Lock from threading import Thread import time lock1 = Lock() lock2 = Lock() def work1(num): lock1.acquire() #lock1 lock up time.sleep(1) print("in work1") lock2.acquire() #lock2 lock up print("work1----") lock2.release() #lock2 unlock lock1.release() #lock1 unlock def work2(num): lock2.acquire() #lock2 lock print("in work2") lock1.acquire() #lock2 lock print("work2-----") lock1.release() #lock1 unlock lock2.release() #lock2 unlock if __name__ == '__main__': t1 = Thread(target=work1,args=(1000000,)) t2 = Thread(target=work2,args=(1000000,)) t1.start() t2.start()
Operation result: