Table of Contents
Introduction
select method
Example:
epoll method:
epoll实例:
Home Backend Development Python Tutorial In-depth analysis of the select module in python

In-depth analysis of the select module in python

Aug 18, 2017 pm 01:55 PM
python select parse

Introduction

The select module in Python focuses on I/O multiplexing and provides three methods: select poll and epoll (the latter two are available in Linux, and windows only supports select). In addition The kqueue method (freeBSD system) is also provided.

select method

The process specifies which file descriptors the kernel listens for (up to 1024 fds) and which events, when no file descriptor events occur. , the process is blocked; when one or more file descriptor events occur, the process is awakened.

When we call select():

1. Context switch to kernel state

2. Copy fd from user space to kernel space

3. The kernel traverses all fds to see if the corresponding event occurs

4. If it does not occur, the process is blocked. When the device driver generates an interrupt or timeout occurs, the process is awakened and traversed again

 5. Return the traversed fd

 6. Copy fd from kernel space to user space

fd:file descriptor file descriptor

fd_r_list, fd_w_list, fd_e_list = select.select(rlist, wlist, xlist, [timeout])

Parameters: Accepts four parameters (the first three are required)

rlist: wait until ready for reading

wlist: wait until ready for writing

xlist: wait for an “exceptional condition”

timeout: timeout time

Return value: three lists

The select method is used to monitor file descriptors (when the file descriptor conditions are not met, select will block). When the status of a file descriptor changes, three lists will be returned

1. When the fd in the sequence of parameter 1 meets the "readable" condition, the changed fd is obtained and added to fd_r_list

2. When the sequence of parameter 2 contains fd, all the fd in the sequence are fd is added to fd_w_list

3. When an error occurs in the fd in the sequence of parameter 3, the fd with the error will be added to fd_e_list

4. When the timeout is empty, then select will block until the monitored handle changes

When the timeout = n (positive integer), then if there is no change in the monitored handle, select will block for n seconds, and then return three nulls list, if the monitored handle changes, it will be executed directly.

Example:

Use select to implement a concurrent server

import socket
import select
 
s = socket.socket()
s.bind(('127.0.0.1',8888))
s.listen(5)
r_list = [s,]
num = 0
while True:
 rl, wl, error = select.select(r_list,[],[],10)
 num+=1
 print('counts is %s'%num)
 print("rl's length is %s"%len(rl))
 for fd in rl:
  if fd == s:
   conn, addr = fd.accept()
   r_list.append(conn)
   msg = conn.recv(200)
   conn.sendall(('first----%s'%conn.fileno()).encode())
  else:
   try:
    msg = fd.recv(200)
    fd.sendall('second'.encode())
   except ConnectionAbortedError:
    r_list.remove(fd)
 
s.close()
Copy after login
import socket
 
flag = 1
s = socket.socket()
s.connect(('127.0.0.1',8888))
while flag:
 input_msg = input('input>>>')
 if input_msg == '0':
  break
 s.sendall(input_msg.encode())
 msg = s.recv(1024)
 print(msg.decode())
 
s.close()
Copy after login
Copy after login

On the server side we can see that we need to keep calling select, which means :

 1 When there are too many file descriptors, copying the file descriptors between user space and kernel space will be very time-consuming

 2 When there are too many file descriptors, the kernel traverses the file descriptors It is also a waste of time

 3 Select only supports a maximum of 1024 file descriptors

poll is not much different from select and will not be introduced in this article

epoll method:

epoll has improved select very well:

 1. The solution of epoll is in the epoll_ctl function. Each time a new event is registered to the epoll handle, all fds will be copied into the kernel instead of repeated copies during epoll_wait. epoll ensures that each fd will only be copied once during the entire process.

 2. epoll will traverse the specified fd during epoll_ctl (this time is essential) and specify a callback function for each fd. When the device is ready and wakes up the waiters on the waiting queue, This callback function will be called, and this callback function will add the ready fd to a ready linked list. The job of epoll_wait is actually to check whether there is a ready fd in this ready list

 3. epoll has no additional restrictions on file descriptors

select.epoll(sizehint=-1, flags= 0) Create an epoll object

epoll.close()

Close the control file descriptor of the epoll object.Close the file descriptor of the epoll object

epoll.closed

True if the epoll object is closed. Check whether the epoll object is closed

epoll.fileno()

Return the file descriptor number of the control fd. Return the file descriptor of the epoll object

epoll.fromfd(fd)

Create an epoll object from a given file descriptor. Create an epoll object based on the specified fd

epoll.register(fd[, eventmask])

Register a fd descriptor with the epoll object. To the epoll object Register fd and the corresponding event in

epoll.modify(fd, eventmask)

Modify a registered file descriptor. Modify the event of fd

epoll.unregister(fd)

Remove a registered file descriptor from the epoll object.Unregister

epoll.poll(timeout=-1, maxevents=-1)

Wait for events. timeout in seconds (float) blocks until the registered fd event occurs, and a dict will be returned with the format: {(fd1,event1),(fd2,event2) ,...(fdn,eventn)}

Event:

EPOLLIN Available for read The readable status symbol is 1

EPOLLOUT Available for write The writable status symbol is 4

EPOLLPRI Urgent data for read

EPOLLERR Error condition happened on the assoc. fd The error status symbol is 8

EPOLLHUP Hang up happened on the assoc. fd Hang status

EPOLLET Set Edge Trigger behavior, the default is Level Trigger behavior 默认为水平触发,设置该事件后则边缘触发

EPOLLONESHOT Set one-shot behavior. After one event is pulled out, the fd is internally disabled

EPOLLRDNORM Equivalent to EPOLLIN

EPOLLRDBAND Priority data band can be read.

EPOLLWRNORM Equivalent to EPOLLOUT

EPOLLWRBAND Priority data may be written.

EPOLLMSG Ignored.

水平触发和边缘触发:

Level_triggered(水平触发,有时也称条件触发):当被监控的文件描述符上有可读写事件发生时,epoll.poll()会通知处理程序去读写。如果这次没有把数据一次性全部读写完(如读写缓冲区太小),那么下次调用 epoll.poll()时,它还会通知你在上没读写完的文件描述符上继续读写,当然如果你一直不去读写,它会一直通知你!!!如果系统中有大量你不需要读写的就绪文件描述符,而它们每次都会返回,这样会大大降低处理程序检索自己关心的就绪文件描述符的效率!!! 优点很明显:稳定可靠

Edge_triggered(边缘触发,有时也称状态触发):当被监控的文件描述符上有可读写事件发生时,epoll.poll()会通知处理程序去读写。如果这次没有把数据全部读写完(如读写缓冲区太小),那么下次调用epoll.poll()时,它不会通知你,也就是它只会通知你一次,直到该文件描述符上出现第二次可读写事件才会通知你!!!这种模式比水平触发效率高,系统不会充斥大量你不关心的就绪文件描述符!!!缺点:某些条件下不可靠

epoll实例:

import socket
import select
 
s = socket.socket()
s.bind(('127.0.0.1',8888))
s.listen(5)
epoll_obj = select.epoll()
epoll_obj.register(s,select.EPOLLIN)
connections = {}
while True:
 events = epoll_obj.poll()
 for fd, event in events:
  print(fd,event)
  if fd == s.fileno():
   conn, addr = s.accept()
   connections[conn.fileno()] = conn
   epoll_obj.register(conn,select.EPOLLIN)
   msg = conn.recv(200)
   conn.sendall('ok'.encode())
  else:
   try:
    fd_obj = connections[fd]
    msg = fd_obj.recv(200)
    fd_obj.sendall('ok'.encode())
   except BrokenPipeError:
    epoll_obj.unregister(fd)
    connections[fd].close()
    del connections[fd]
 
s.close()
epoll_obj.close()
Copy after login
import socket
 
flag = 1
s = socket.socket()
s.connect(('127.0.0.1',8888))
while flag:
 input_msg = input('input>>>')
 if input_msg == '0':
  break
 s.sendall(input_msg.encode())
 msg = s.recv(1024)
 print(msg.decode())
 
s.close()
Copy after login
Copy after login

The above is the detailed content of In-depth analysis of the select module in python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI has started to provide developers with access to extended context windows and cost-saving features, starting with the Gemini 1.5 Pro large language model (LLM). Previously available through a waitlist, the full 2 million token context windo

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download DeepSeek Xiaomi? Search for "DeepSeek" in the Xiaomi App Store. If it is not found, continue to step 2. Identify your needs (search files, data analysis), and find the corresponding tools (such as file managers, data analysis software) that include DeepSeek functions.

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

The key to using DeepSeek effectively is to ask questions clearly: express the questions directly and specifically. Provide specific details and background information. For complex inquiries, multiple angles and refute opinions are included. Focus on specific aspects, such as performance bottlenecks in code. Keep a critical thinking about the answers you get and make judgments based on your expertise.

How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

Just use the search function that comes with DeepSeek. Its powerful semantic analysis algorithm can accurately understand the search intention and provide relevant information. However, for searches that are unpopular, latest information or problems that need to be considered, it is necessary to adjust keywords or use more specific descriptions, combine them with other real-time information sources, and understand that DeepSeek is just a tool that requires active, clear and refined search strategies.

How to program deepseek How to program deepseek Feb 19, 2025 pm 05:36 PM

DeepSeek is not a programming language, but a deep search concept. Implementing DeepSeek requires selection based on existing languages. For different application scenarios, it is necessary to choose the appropriate language and algorithms, and combine machine learning technology. Code quality, maintainability, and testing are crucial. Only by choosing the right programming language, algorithms and tools according to your needs and writing high-quality code can DeepSeek be successfully implemented.

How to use deepseek to settle accounts How to use deepseek to settle accounts Feb 19, 2025 pm 04:36 PM

Question: Is DeepSeek available for accounting? Answer: No, it is a data mining and analysis tool that can be used to analyze financial data, but it does not have the accounting record and report generation functions of accounting software. Using DeepSeek to analyze financial data requires writing code to process data with knowledge of data structures, algorithms, and DeepSeek APIs to consider potential problems (e.g. programming knowledge, learning curves, data quality)

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

See all articles