Process such as QQ to a whole form of exposure to the operating system management, which contains a variety of resource calls, memory management, network interface calls, etc., process is a collection of various resource management
Thread: is the smallest dispatch unit of operating system, is a combination of a series of instructions
Process to operate the CPU, you must create a thread
1 so threads in the same process are sharing the same block of memory space
Data for 22 sub-processes is not shared and cannot be accessed by each other, and thread data is shared
3. The thread of the same process can communicate, data sharing, information transfer, two processes want to communicate, must pass an intermediary agent
4. Creating a new thread is simple, creating a new process requires a clone of its parent process
5. One thread can control and manipulate other threads, but the process can only manipulate child processes
6. Modifications to the main thread may affect other threads, but the process will only manipulate the child processes
The first way to create a process: a function call
1 Importthreading, time2 3 defrun (n, run_time):4 Print('Task Run%s'%N)5 time.sleep (run_time)6 Print('The run%s'%N)7 8 9 Ten One Am = Threading. Thread (Target=run, args= (5, 1))#Child Thread - -M1 =threading. Thread (Target=run, args= (2, 3)) the - M.start () - M1.start () - +M1.join ()#executes the child thread M1 -M.join ()#executes the child thread M2 + A at Print('Alex')
The second way to create a process: Inherit threading. Thread
Importthreading, timeclassMyThread (Threading. Thread):def __init__(self, N, run_time): Threading. Thread. __init__(self) SELF.N=N self.time=Run_timedefRun (self):Print('Task run%s'%SELF.N) time.sleep (self.time)Print('The run%s'%SELF.N) M= MyThread (1, 5) M1= MyThread (3, 2) M.start () M1.start () M.join (timeout=2)#Executes in two seconds and goes down .M1.join (1)#Execute one second and go down .Print('Task Done')
Two ways to process and create threads (threading. Thread)