1.python Multi-threaded programming
A process is made up of several threads, and a process has at least one thread. Because threads are the execution unit that is directly supported by the operating system, the high-level language is usually built into multi-threaded support, and Python is no exception, and Python threads are true POSIX thread rather than simulated threads.
Python's standard library provides two modules: _thread and Threading,thread are low-level modules, and threading is an advanced module that encapsulates thread. In most cases, we just need to use the Advanced module threading. The following is a routine that uses the threading module to implement multithreaded programming:
Import time, threading
# code executed by new thread:
def loop ():
print (' thread%s is running ... '% threading.current_thread () . name)
n = 0 while
N < 5:
n = n + 1
print (' thread%s >>>%s '% (Threading.current_thread (). Nam E, N)
time.sleep (1)
print (' thread%s ended. '% Threading.current_thread (). Name)
print (' thread%s is Running. '% Threading.current_thread (). Name)
t = Threading. Thread (Target=loop, name= ' Loopthread ')
T.start ()
t.join ()
print (' thread%s ended. ') Threading.current_thread (). Name)
The results of the operation are as follows:
Thread Mainthread is running
... Thread Loopthread is running
... Thread loopthread >>> 1
thread loopthread >>> 2
thread loopthread >>> 3
thread Loopthread >>> 4
thread loopthread >>> 5
thread loopthread ended.
Thread Mainthread ended.
Because any process will start a thread by default, we call it the main thread, and the main thread can start a new one, and the Python threading module has a current_thread () function that always returns an instance of the current thread. The primary thread instance is named Mainthread, and the child thread name is specified at creation time, and we use Loopthread to name the child thread. The name is only used to display when printing, there is no other meaning, if the name Python automatically named the thread thread-1,thread-2 ...
The start function is the start thread, and when the Start method is invoked, the thread starts executing. Join is a blocking method that blocks the thread that calls the join function until the corresponding thread has finished executing.
2. Use of locks in multiple threads
The biggest difference between multithreading and multiple processes is that in multiple processes, the same variable, each of which has a copy stored in each process, does not affect each other, and in multiple threads, all variables are shared by all threads, so any variable can be modified by any thread, so The greatest danger of sharing data between threads is that multiple threads change a variable at the same time, and the content is messed up.
Import time, Threading
# Assuming this is your bank deposit:
balance = 0
lock = Threading. Lock ()
def change_it (n):
# First Save and then fetch, the result should be 0:
global balance
balance = balance + N
balance = Balance-n
def run_thread (N): for
I in range (100000):
# to get the lock first:
lock.acquire ()
try: # be
assured:
Change_it (n)
finally:
# After the change, be sure to release the Lock:
lock.release ()
t1 = Threading. Thread (Target=run_thread, args= (5,))
t2 = Threading. Thread (Target=run_thread, args= (8,))
T1.start () T2.start () T1.join () t2.join ()
Print ( Balance
The result of the program running is 0.
When multiple threads execute Lock.acquire () at the same time, only one thread succeeds in acquiring the lock, and then continues executing the code, and the other threads continue to wait until the lock is obtained. The thread that gets the lock must release the lock when it is finished, otherwise the thread that is waiting for the lock will wait forever and become a dead thread. So we use try...finally to make sure the lock will be released.
The advantage of the lock is to ensure that a piece of key code can only be executed from beginning to end by a single thread, the disadvantages of course many, the first is to prevent multithreading concurrent execution, a piece of code containing the lock can only be executed in single-threaded mode, the efficiency is greatly reduced. Second, because multiple locks can exist, different threads hold different locks, and attempts to acquire the other's held locks may cause deadlocks, causing multiple threads to hang, neither executing nor ending, and can only be forced to terminate by the operating system.
Learning materials refer to:
Http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 00143192823818768cd506abbc94eb5916192364506fa5d000