1, multi-process, multiprocessing module,
Inter-process communication: queue[Queue],pipes[Tube]
2, multi-threading,
Note: Thread common variables, confusion
Workaround Lock: Because there is only one lock, when you want to perform a unified function, only if the unlock
Perform.
balance = 0lock = threading. Lock () def Run_thread (n): for i in range (100000 # first to get the lock: Lock.acquire () try : # change it safely: Change_it (n) finally< /span> You must release the lock when you are finished: lock.release ()
3, ThreadLocal a global variable.
Look at the code:
ImportThreading#To create a global threadlocal object:Local_school =threading.local ()defprocess_student ():#gets the student associated with the current thread:std =local_school.studentPrint('Hello,%s (in%s)'%(Std, Threading.current_thread (). Name))defProcess_thread (name):#binding the threadlocal student:Local_school.student =name Process_student () T1= Threading. Thread (target= process_thread, args= ('Alice',), name='thread-a') T2= Threading. Thread (target= process_thread, args= ('Bob',), name='Thread-b') T1.start () T2.start () T1.join () T2.join ()
Source: http://www.liaoxuefeng.com
"Python" [Processes and threads] multi-process, multi-threading, ThreadLocal, Process vs. threads, distributed processes