On a CPU (one core) of the computer,
The operation of the program is concurrent operation, scheduling algorithm called time slice rotation method, also called polling method
On a multi-CPU (multi-core) computer, a CPU running a program, just the number of programs run less than the number of cores, the program is parallel
Concurrency: appears to execute together while it is occurring
parallel: real execution, while in progress
Concept of the process:
A computer program is an executable binary (or other type) file that is stored on disk
They will have their own life cycle only when they are loaded into memory and called by the operating system.
The process represents a program that is executing
Each program has its own address space, memory, data stack, and other supporting materials for tracking execution
The operating system is responsible for the execution of all processes on it
The operating system allocates execution time reasonably for these processes
Thread Concept:
Threads are called lightweight processes
Similar to the process, but they are executed under different processes. And they will share the same context
When other threads execute, he can be preempted (interrupted), and temporarily suspend (sleep)---------concession
The polling scheduling mechanism for threads is similar to the polling schedule for a process. It's just that the scheduler is not responsible for the operating system, but the Python interpreter is responsible for
Scheduling relationships between CPUs, processes, and threads
Cpu--> Process--Threading
Using processes and threads to implement concurrent servers
Threads: (Gil Global interpreter Lock: switch when blocking is encountered)
fromSocketImport*ImportThreadingserver=socket () Server.bind (("', 9999)) Server.listen (1000)deffunc (conn): whiletrue:recv_date= CONN.RECV (1024) ifrecv_date:Print(Recv_date.decode ('UTF8')) Conn.send (recv_date)Else: Conn.close () Break whileTrue:conn, addr=server.accept () W= Threading. Thread (Target=func, args= (conn,))#turn on a child threadW.start ()
Process:
ImportMultiprocessing fromSocketImport*Server=socket () Server.bind (("', 9999)) Server.listen (10)deffunc (conn): whiletrue:recv_date= CONN.RECV (1024) ifrecv_date:Print(Recv_date.decode ('UTF8')) Conn.send (recv_date)Else: Conn.close () Break whileTrue:conn, addr=server.accept () W= multiprocessing. Process (Target=func, args= (conn,))#turn on a child threadW.start ()
Thread processes under Python, and how to implement a concurrent server