Thread processes under Python, and how to implement a concurrent server

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.