原文标题:python中select怎么用
Python的select()方法直接调用操作系统的IO接口,它监控sockets,open files, and pipes(所有带fileno()方法的文件句柄)何时变成readable 和writeable, 或者通信错误,select()使得同时监控多个连接变的简单,并且这比写一个长循环来等待和监控多客户端连接要高效,因为select直接通过操作系统提供的C的网络接口进行操作,而不是通过Python的解释器。
注意:Python的select()方法适用于UNIX操作系统,不支持Windows操作系统
接下来通过echo server例子要以了解select 是如何通过单进程实现同时处理多个非阻塞的socket连接的。
import select import socket import sys import Queue # Create a TCP/IP socket server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setblocking(0) # Bind the socket to the port server_address = ('localhost', 10000) print >>sys.stderr, 'starting up on %s port %s' % server_address server.bind(server_address) # Listen for incoming connections server.listen(5)
[版权声明]
本文标题:python中select怎么用作者:python学习
本文链接:http://www.ccpit.org.cn/jichu/jc6101.html——此文章系本站原创/整理,转载请标明出处、原标题、链接