Lock component
When we use a multi-process to read and write files, if a process is to write files, a process is to read the file, if two files at the same time, it is certainly not, it must be the end of the file write, you can read the operation. Or when multiple processes share some resources, and only one process can access them, there is a lock mechanism to control.
Acquire ()
Locked ()
Release ()//unlock, use the front thread must have been locked, otherwise throw an exception
Lock=thread.allocate_lock (): Generate Lock Object
Lock.acquire (): Plus lock
Thread uses lock object
Lock.release (): Thread Dispatch release lock
View locked () status
Get a lock, release the lock on the set
Threading high-level multithreaded modules
Threading does not require process control to control threads
Threading. Thread: Class
Member Methods:
Start () startup
Run () override
Join () Blocking
GetName ()
SetName ()
Isdaemon () to determine if a thread ended with the main thread
Setdaemon () sets the thread to end with the main thread
Shared memory
Python's multiprocessing module also provides us with shared memory operations.
General variables are not able to communicate between processes, multiprocessing provides us with the value and array modules, they can be used in the process of non-pass together
Example:
1 ImportMultiprocessing2 3 #lock = multiprocessing. Lock ()4 #Lock.acquire () get lock5 #lock.release () release lock6 #With Lock: Locking7 8 Import Time9 Ten #Value () One #Array () A defAdd (number,add_value,lock): - Lock.acquire () - Try: the Print("init add{0} number = {1}". Format (add_value,number.value)) - forIinchXrange (1,6): -Number.value + =Add_value - Print("########### #add {0} has added#######". Format (add_value)) +Time.sleep (1) - Print("add{0} number = {1}". Format (add_value,number.value)) + exceptException as E: A Raisee at finally: - lock.release () - defChange (arr): - forIinchRange (len (arr)): -Arr[i] =-Arr[i] - in if __name__=="__main__": -Lock = multiprocessing. Lock ()#Locking toNumber = multiprocessing. Value ('I', 0)#Shared Memory +arr = multiprocessing. Array ('I', Range (10))#Shared Memory Array - Print(arr[:]) theP1 = multiprocessing. Process (target=add,args= (number,1, lock)) *P2 = multiprocessing. Process (Target=add, args= (number, 3, lock)) $P3 = multiprocessing. Process (Target=change, args=(arr,))Panax Notoginseng P1.start () - #P1.join () the P2.start () + ##p2. Join () AP3.start ()#Start the P3.join () + Print(arr[:]) - Print("Main End") $ $ Output Result: -[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -init add1 number =0 the ########### #add1 has added####### -[0,-1,-2,-3,-4,-5,-6,-7,-8,-9]Wuyi Main End theADD1 Number = 1 - ########### #add1 has added####### WuADD1 Number = 2 - ########### #add1 has added####### AboutADD1 Number = 3 $ ########### #add1 has added####### -ADD1 Number = 4 - ########### #add1 has added####### -ADD1 Number = 5 Ainit ADD3 number = 5 + ########### #add3 has added####### theADD3 Number = 8 - ########### #add3 has added####### $ADD3 Number = 11 the ########### #add3 has added####### theADD3 Number = 14 the ########### #add3 has added####### theADD3 Number = 17 - ########### #add3 has added####### inADD3 Number = 20
day37--multi-process lock, multi-process shared memory