A thread is a multi-tasking programming mode, you can use a multi-core computer resources. Also known as the thread lightweight process
* A thread is the smallest unit of computer core allocation process * A process can contain multiple threads * thread is running, but also consume computer resources. Process multiple threads share its resources and space * thread also has its own unique resource properties, such as instruction set, TID, etc. * Whether to create or delete a thread or process running resource consumption is less than * parallel execution across multiple threads, and do not interference
threading threading module
from threading import Thread
t = Thread(target, [, args], [kwargs])
Create a thread object
target binding thread function
tuple args parameter passing position to the thread function
kwargs dictionary to the thread function key parameter passing
t.start () start the thread
t.join ([timeout]) collection threads
import threading import os a = 1 #
Thread functiondef music(): print("
Process pid No.", os.getpid()) global a print("a = ",a) a = 10000 t = threading.Thread(target=music) #
Create a thread objectt.start() #
Start a threadprint("
Process pid No.", os.getpid()) t.join() #
Recycling threadprint("Main a:",a) #
Process pid No. 12549#
Process pid No. 12549# a = 1 # Main a: 10000
os.getpid get that number pid process, thread is a member of the process.
Variable thread change is the process variables. It did not open up a new space.
t.is_alive () View thread state
t.name name of the default threads Thread-1
t.setName () Set the thread name
threading.currentThread () Gets the current thread object
1 from threading import Thread,currentThread 2 from time import sleep 3 4 #
Thread function5 def fun(sec): 6 print("
Thread Attributes Test") 7 sleep(sec) 8 #
Gets thread object getName () Get the name9 print("
% S thread end"%currentThread().getName()) 10 11 thread =  12 13 for i in range(3): 14 t = Thread(target = fun,name = "tedu%d"%i,\ 15 args = (3,)) 16 thread.append(t) 17 t.start() 18 print(t.is_alive()) #
View the process status19 20 thread.setName('Tarena') #
Set the thread name21 print(thread.name) #
Get Thread name22 23 #
Recycling thread24 for i in thread: 25 i.join() 26 27 #
Thread Attributes Test28 # True 29 #
Thread Attributes Test30 # True 31 #
Thread Attributes Test32 # True 33 # tedu2 34 #
Tarena end of the thread35 #
tedu0 end of the thread36 #
tedu2 end of the thread
By default, the end of the main thread does not affect threads branch, set to True if the main thread exit branch thread will quit
t.daemon = True
Set the thread daemon property before the start; after general settings daemon does not use the join
from threading import Thread from time import sleep def fun(): sleep(3) print("
Thread Attributes Test") t = Thread(target=fun, name = "Tarena") #
The main thread exit branch threads also quitt.setDaemon(True) t.start() t.setName("Tedu") print("Name:",t.getName()) #
Thread nameprint("Alive:",t.is_alive()) #
Thread Life Cycleprint("is Daemon",t.isDaemon()) #
With the main branch process exit process
Custom thread class
Thread class inheritance
__init__ method of operation for the Thread class parent class attributes
Override the run method
Instantiate an object
Automated call start method executes the run
Call to join the recovery thread
from threading import Thread class ThreadClass(Thread): #
Override the parent class initdef __init__(self, *args, **kwargs): self.attr = args super().__init__() #
Load the parent class initdef fun1(self): print("
Function 1") def fun2(self): print("
Function 2") #
Rewrite run, the logical calldef run(self): self.fun1() self.fun2() t = ThreadClass("abc") t.start() t.join() #
Inter-thread communication method
1. The communication method: using global variables between threads communicate
2. Shared resource contention
Sharing resources: multiple processes or threads can operate is called a shared resource. Operation code segment to a shared resource called the critical zone.
Impact: disorderly operation of shared resources could bring chaos data, or operator error. At this time, often require synchronization mechanism coordinating the sequence of operations are mutually exclusive.
3. Synchronize mutual exclusion mechanism
Synchronization: Synchronization is a collaborative relationship, to complete the operation, between multiple processes or threads to form a coordinated, orderly steps necessary to perform the operation.
Mutex: mutual exclusion is a constraint relationship, when a process or thread locking possession of resources will be processed at this time other process threads can not operate the resources to operate until after unlocking.
Mutex thread synchronization method
from threading import Event
e = Event () creates a thread event objects
e.wait ([timeout]) blocked waiting to be set e
e.set () provided e, so that the end blocking wait
e.clear () is not set back to the state that the e
e.is_set () is set or view the current e
from threading import Thread,Event s = None #
For communicatione = Event() #
Create event objectsdef 杨子荣(): print("
Zirong come to worship the hills") global s s = "
King cover ground tiger" e.set() #
Set of et = Thread(target=杨子荣) t.start() print("
Said password is one of us") e.wait() #
Blocking wait password sayif s == '
King cover ground tiger': print("
River town pagoda demon") print("
Confirmed eyes, you are the right person") else: print("
kill him...") t.join()
Thread Lock Lock
from threading import Lock
lock = Lock () to create a lock object
lock.acquire () If the lock is already locked locked again call blocks
lock.release () Unlock
with lock: # lock
automatically unlock code block ends with
from threading import Thread,Lock a = b = 0 lock = Lock() #
Defined lockdef value(): while True: lock.acquire() #
Lockif a != b: print("a = %d,b = %d"%(a,b)) lock.release() #
Unlockt = Thread(target = value) t.start() while True: #
Lockwith lock: a += 1 b += 1 #
GIL problem python thread
The GIL (Global Interpreter Lock)
python — “supports threaded operation —” synchronization and mutual exclusion of IO – “locked —-” super lock to lock interpreter
Consequences: an interpreter, explained only one thread at a time, then other threads need to wait. Greatly reducing the efficiency of the implementation python thread
python GIL solutions to problems * modified * c interpreter to make use of multiple processes in parallel operation * python thread can use high latency IO case of a multi obstruction * do not use cpython c # java do interpreter
Were tested multi-process multi-threaded single process to perform the same operation IO and CPU
Computationally intensivedef count(x,y): c = 0 while c < 7000000: x += 1 y += 1 c += 1 #
io-intensivedef write(): f = open("test.txt",'w') for x in range(2000000): f.write("hello world\n") f.close() def read(): f = open("test.txt") lines = f.readlines() f.close()
Single-process proceduresfrom test import * import time # t = time.time() # for i in range(10): # count(1,1) # print("Line cpu:",time.time() - t) t = time.time() for i in range(10): write() read() print("Line IO:",time.time() - t)
Line cpu: 8.15166711807251
Line IO: 6.841825246810913
from test import * import threading import time counts =  t = time.time() for x in range(10): th = threading.Thread(target = count,args = (1,1)) th.start() counts.append(th) for i in counts: i.join() print("Thread cpu",time.time() - t)
from test import * import threading import time counts =  def io(): write() read() t = time.time() for x in range(10): th = threading.Thread(target = io) th.start() counts.append(th) for i in counts: i.join() print("Thread IO",time.time() - t)
Thread cpu 8.414522647857666
Thread IO 6.023292541503906
from test import * import multiprocessing import time counts =  t = time.time() for x in range(10): th = multiprocessing.Process\ (target = count,args = (1,1)) th.start() counts.append(th) for i in counts: i.join() print("Process cpu",time.time() - t)
from test import * import multiprocessing import time counts =  def io(): write() read() t = time.time() for x in range(10): th = multiprocessing.Process(target = io) th.start() counts.append(th) for i in counts: i.join() print("Process IO",time.time() - t)
Process cpu 4.079084157943726
Process IO 3.2132551670074463
Process and thread the differences and connections
Both are multi-task programmatically, you can use multi-core computer
Creating than deleting threads of a process consumes more computer resources
Independent process space, data security is good, there is a special inter-process communication method
Thread communication using global variables, more simple, but requires synchronization mutex operation
A process can contain multiple threads, threads share the process of space resources
Process threads execute independently, with its own unique resources such as property, id, command sets, etc.
A process more concurrent tasks, relatively simple, suitable for use multithreading
If the data is more complex procedures, especially multiple tasks may be more communication time, taking into account the complexity of the synchronization exclusive use of threads
There are obvious differences in multiple tasks, and when the functional separation is not necessarily written to a process
Consider using python threading issues GIL