Multithreaded Socket model

Source: Internet
Author: User

With each client access, the server generates a process to serve the client, and when the client is many, it consumes a lot of system resources as many processes.

From socket Import *
Import time
Import OS
Import Sys
Import threading
def client_deal (Client,client_info):
     while True:
        msg = CLIENT.RECV (1024)
        if "':
            Client.close ()
            Break
        Print ' recv= ', MSG,' from: ', Cleint_info
        Client.send (' echo ' + msg)
        if ' Q ':
            Client.close ()
            Break
def thread_process (Client,client_info):
        P_thread = Threading. Thread (target=client_deal,args= (Client,client_info))
        P_thread.setdaemon (True)
        P_thread.start ()
        Print "Start one thread"
if ' __main__ ':
    Tcp_server = socket (af_inet,sock_stream)
    Tcp_server.bind ((' localhost ', 5001))
    Tcp_server.listen (5)
     while True:
        Print ' pid= ', Os.getpid (),"Wait client"
        Client,client_info = Tcp_server.accept ()
        Print Client_info
        Client_process (Client,client_info)
    Tcp_server.close ()

Multithreaded Socket model

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.