Processes (Process)
A process is an instance of a program's memory (a process is an execution activity of a program, it is dynamic)
The implementation of multi-channel programming is to open the instance of multiple programs in memory at the same time, giving an external illusion of concurrency.
Defects in the process:
1, the process can only do one thing at the same time, if you want to do two things at the same time, the process is powerless.
2, when the process is executing a task, if it is blocked (for example, waiting for I/O), the entire program will be suspended
Threads (thread)
A thread is the smallest unit that the operating system can perform operations on, is contained in a process, is the actual operating unit in the process, and a thread refers to a single sequential control of the process.
A process can be concurrent with multiple threads, and each thread performs a different task.
The difference between a process and a thread:
1, shared memory addresses between threads (with process creation)
2, threads can communicate directly between them, and processes cannot
3, creating a thread is very quick and easy, creating a process that requires copying the complete program data
4, threads can control other threads in the same process, and processes can only control child processes
Python implements multi-threaded modules:
Threading
Simple multi-threaded concurrency program
1, call directly
#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportThreading,timedeffoo (num):Print("running on number {}.". Format (num)) Time.sleep (5)if __name__=='__main__': T1= Threading. Thread (target=foo,args= (1,)) T2= Threading. Thread (target=foo,args= (1,)) T1.start () T2.start ( )Print(T1.getname ())Print(T2.getname ())
2, inheriting type
#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportThreading,timeclassMythread (Threading. Thread):def __init__(self,num): Threading. Thread.__init__(self) self.num=NumdefRun (self):Print("Running at number%s."%self.num) Time.sleep (5) T1= Mythread (1) T2= Mythread (2) T1.start () T2.start ()
Regardless of the form in which multiple threads are implemented, the threads are concurrent at run time.
Python Multithreading (1)