【select模块】select IO多路复用和select实现FTP
admin
2023-07-23 18:01:51
0次

select是全平台通用的IO多路复用模块。最大连接数:1024。

poll和epoll没有最大连接数限制,但只能用在linux平台。

selectors是再封装模块,推荐使用。下篇会讨论。

select.select(rlist, wlist, xlist[, timeout])

  • This is a straightforward interface to the Unix select() system call. The first three arguments are sequences of ‘waitable objects’: either integers representing file descriptors or objects with a parameterless method named fileno() returning such an integer:

    Empty sequences are allowed, but acceptance of three empty sequences is platform-dependent. (It is known to work on Unix but not on Windows.)  The optional timeout argument specifies a time-out as a floating point number in seconds.  When the timeout argument is omitted the function blocks until at least one file descriptor is ready.  A time-out value of zero specifies a poll and never blocks.

    The return value is a triple of lists of objects that are ready: subsets of the first three arguments.  When the time-out is reached without a file descriptor becoming ready, three empty lists are returned.

    Among the acceptable object types in the sequences are Python file objects (e.g. sys.stdin, or objects returned by open() or os.popen()), socket objects returned by socket.socket().  You may also define a wrapper class yourself, as long as it

    • rlist: wait until ready for reading

    • wlist: wait until ready for writing

    • xlist: wait for an “exceptional condition” (see the manual page for what your system considers such a condition)

方法、属性参数作用示例
select(rlist,wlist,rlist,[timout=1])



poll()
没人用了,已经升级为epoll


epoll(sizehint = -1,flags=0)

sizehint informs epoll about the expected number of events to be registered.  It must be positive, or-1to use the default. It is only used on older systems where epoll_create1() is not available; otherwise it has no effect (though its value is still checked).

flags is deprecated and completely ignored.  However, when supplied, its value must be 0 or select.EPOLL_CLOEXEC, otherwise OSError is raised.


(Only supported on Linux 2.5.44 and newer.) Return an edge polling object, which can be used as Edge or Level Triggered interface for I/O events.

devpoll()

 

(Only supported on Solaris and derivatives.)  Returns a /dev/poll polling object; see section /dev/poll Polling Objects below for the methods supported by devpoll objects.
kevent()

select.kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)
  • (Only supported on BSD.)  Returns a kernel event object; see section Kevent Objects below for the methods supported by kevent objects.


kqueue()
(Only supported on BSD.)  Returns a kernel queue object; see section Kqueue Objects below for the methods supported by kqueue objects.


import socket
import os
import select
import queue
import json


class SelectFTP(object):
    def __init__(self, ip, port):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setblocking(0)
        self.server.bind((ip, port))
        self.server.listen(20)
        

        self.inputs = [self.server]
        self.outputs = []
        self.file_attr = {}
        # file_attr format:file_attr[socket]:{func:'', filename:'', len:999, recv_len:0}      
        self.socket_queue = {}

    
    def upload(self, sock, write_data):
        # if os.path.isfile(self.file_attr[sock]['filename']):
        with open(self.file_attr[sock]['filename'], 'a+') as file:
            file.write(data)
            self.file_attr[sock][recv_len] += len(write_data)
            if self.file_attr[sock][recv_len] == self.file_attr[sock][len]:
                del self.file_attr[sock]
                file.close()
        
   
    def download(self, sock, *args):
        pass
        
    def run(self):
        while self.inputs:
            read_active, read_output, exception = select.select(self.inputs, self.outputs, self.inputs)
            
            for fd in read_active:
                if fd is server:
                    conn, addr = fd.accept(1024)
                    conn.setblocking(0)
                    self.inputs = self.inputs.append(conn)
                    self.socket_queue[fd] = queue.Queue()
                    
                else:
                    recv_data = fd.recv(1024)

                    if recv_data:
                        data = json.loads(recv_data.decode())
                        
                        if fd not in self.file_attr.keys:
                            self.file_attr[fd] = data
                        
                        else:
                            try:
                                self.socket_queue.put_nowait(data)
                                if fd not in self.outputs:
                                    self.outputs.append(fd)
                            except Exception as e:
                                print(e)
                            
                    else:
                        self.inputs.remove(fd)
                        if fd in self.outputs:
                            self.outputs.remove(fd)
                        del self.socket_queue[fd]
                        
                    send_data = 
                        
            for fd in read_output:
                try:
                    message = self.socket_queue.get_nowait()
                except queue.Empty:
                    self.outputs.remove(fd)
                    print('wait...')
                else:
                    getattr(self.file_attr[fd]['func'])(fd, message)

 

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...