首页 > 代码库 > How To Use Linux epoll with Python

How To Use Linux epoll with Python

http://scotdoyle.com/python-epoll-howto.html

 

Line 1: The select module contains the epoll functionality.Line 13: Since sockets are blocking by default, this is necessary to use non-blocking (asynchronous) mode.

  

import socket, selectEOL1 = b‘\n\n‘EOL2 = b‘\n\r\n‘response  = b‘HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n‘response += b‘Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n‘response += b‘Hello, world!‘serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)serversocket.bind((‘0.0.0.0‘, 8080))serversocket.listen(1)serversocket.setblocking(0)epoll = select.epoll()epoll.register(serversocket.fileno(), select.EPOLLIN)try:   connections = {}; requests = {}; responses = {}   while True:      events = epoll.poll(1)      for fileno, event in events:         if fileno == serversocket.fileno():            connection, address = serversocket.accept()            connection.setblocking(0)            epoll.register(connection.fileno(), select.EPOLLIN)            connections[connection.fileno()] = connection            requests[connection.fileno()] = b‘‘            responses[connection.fileno()] = response         elif event & select.EPOLLIN:            requests[fileno] += connections[fileno].recv(1024)            if EOL1 in requests[fileno] or EOL2 in requests[fileno]:               epoll.modify(fileno, select.EPOLLOUT)               print(‘-‘*40 + ‘\n‘ + requests[fileno].decode()[:-2])         elif event & select.EPOLLOUT:            byteswritten = connections[fileno].send(responses[fileno])            responses[fileno] = responses[fileno][byteswritten:]            if len(responses[fileno]) == 0:               epoll.modify(fileno, 0)               connections[fileno].shutdown(socket.SHUT_RDWR)         elif event & select.EPOLLHUP:            epoll.unregister(fileno)            connections[fileno].close()            del connections[fileno]finally:   epoll.unregister(serversocket.fileno())   epoll.close()   serversocket.close()

  

epoll的register

 |  register(...) |      register(fd[, eventmask]) -> None |       |      Registers a new fd or modifies an already registered fd. |      fd is the target file descriptor of the operation. |      events is a bit set composed of the various EPOLL constants; the default |      is EPOLL_IN | EPOLL_OUT | EPOLL_PRI. |       |      The epoll interface supports all file descriptors that support poll.

  

Epoll的eventmask

http://man7.org/linux/man-pages/man2/poll.2.html

              POLLIN There is data to read.              POLLPRI                     There is urgent data to read (e.g., out-of-band data on                     TCP socket; pseudoterminal master in packet mode has                     seen state change in slave).              POLLOUT                     Writing is now possible, though a write larger that the                     available space in a socket or pipe will still block                     (unless O_NONBLOCK is set).              POLLRDHUP (since Linux 2.6.17)                     Stream socket peer closed connection, or shut down                     writing half of connection.  The _GNU_SOURCE feature                     test macro must be defined (before including any header                     files) in order to obtain this definition.              POLLERR                     Error condition (output only).              POLLHUP                     Hang up (output only).              POLLNVAL                     Invalid request: fd not open (output only).

  

How To Use Linux epoll with Python