1. Python Multithreading
1 #Encoding=utf-82 """3 Python multi-threading, not really multithreaded4 Global Lock: At a specified time, there is only one thread running5 6 7 """8 ImportThreading9 Import TimeTen One defTest (P): ATime.sleep (0.1) - PrintP - the #A = threading. Thread (target=test) - #B = Threading. Thread (target=test) - #A.start () - #B.start () + # - #A.join () + #B.join () A atLST =[] - forIinchXrange (0, 15): -th = Threading. Thread (Target=test, args=[i]) - lst.append (TH) - - forIinchLST: in I.start () - to + forIinchLST: - I.join () the Print "PRIMARY thread End!!!"
2, the lock under Python
1 #Encoding=utf-82 3 """4 1, Python global lock: GLT5 python multithreading at any moment only one thread is running, it is thread-safe6 """7 8 ImportThreading9 Tennum =0 One deft (): A GlobalNum -Num +=1 - PrintNum the - forIinchXrange (0, 10): -D = Threading. Thread (target=t) - D.start () + - + Import Time AB_time =time.time () at_a = Threading. Thread (target=t) -_b = Threading. Thread (target=t) - _a.start () - _b.start () - - _a.join () in _b.join () - to PrintTime.time ()-B_time + - """ the Locking: Acquire () * Unlock: Release () $ Rlock () can be re-entered lockPanax Notoginseng - the """ + ImportThreading AMlock =Threading. Lock () the #Mlock = Threading. Rlock () + -num_01 =0 $ defA (): $ Globalnum_01 - Mlock.acquire () -Num_01 + = 1 the mlock.release () - Printnum_01Wuyi the forIinchXrange (0, 10): -D = Threading. Thread (target=a) Wu D.start () -
Python Thread and lock