Python select does not block socket

Source: Internet
Author: User
#! /Usr/bin/ENV Python #-*-encoding: UTF-8-*-import socketimport selecthost = "" Port = 50000 S = socket. socket (socket. af_inet, socket. sock_stream) s. BIND (host, Port) s. listen (5) print "begin... "While 1: infds, outfds, errfds = select. select ([s,], [], [], 5) # If the infds status changes, process it. Otherwise, if Len (infds) is ignored )! = 0: clientsock, clientaddr = S. accept () infds_c, outfds_c, errfds_c = select. select ([clientsock,], [], [], 3) If Len (infds_c )! = 0: Buf = clientsock. Recv (8196) If Len (BUF )! = 0: Print (BUF) clientsock. Close () print "clientsock closed" print "no data coming"

An article on zookeeperArticleDescribes the non-blocking Socket Type of Python.

In the original generation of primary keys, a new SELECT statement is added and two select statements are used,

The first select has many clients to connect. If there is no client to connect after five seconds, it shows "no data coming"

The second select is to directly close the client if no message is sent after 3 seconds, which will not cause blocking.

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.