⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectpoll.py

📁 BitTorrentABC-Linux-V.2.4.3源码
💻 PY
字号:
# Written by Bram Cohen# see LICENSE.txt for license informationfrom select import select, errorfrom time import sleepfrom types import IntTypefrom bisect import bisectPOLLIN = 1POLLOUT = 2POLLERR = 8POLLHUP = 16class poll:    def __init__(self):        self.rlist = []        self.wlist = []            def register(self, f, t):        if type(f) != IntType:            f = f.fileno()        if (t & POLLIN) != 0:            insert(self.rlist, f)        else:            remove(self.rlist, f)        if (t & POLLOUT) != 0:            insert(self.wlist, f)        else:            remove(self.wlist, f)            def unregister(self, f):        if type(f) != IntType:            f = f.fileno()        remove(self.rlist, f)        remove(self.wlist, f)    def poll(self, timeout = None):        if self.rlist != [] or self.wlist != []:            r, w, e = select(self.rlist, self.wlist, [], timeout)        else:            sleep(timeout)            return []        result = []        for s in r:            result.append((s, POLLIN))        for s in w:            result.append((s, POLLOUT))        return resultdef remove(list, item):    i = bisect(list, item)    if i > 0 and list[i-1] == item:        del list[i-1]def insert(list, item):    i = bisect(list, item)    if i == 0 or list[i-1] != item:        list.insert(i, item)def test_remove():    x = [2, 4, 6]    remove(x, 2)    assert x == [4, 6]    x = [2, 4, 6]    remove(x, 4)    assert x == [2, 6]    x = [2, 4, 6]    remove(x, 6)    assert x == [2, 4]    x = [2, 4, 6]    remove(x, 5)    assert x == [2, 4, 6]    x = [2, 4, 6]    remove(x, 1)    assert x == [2, 4, 6]    x = [2, 4, 6]    remove(x, 7)    assert x == [2, 4, 6]    x = [2, 4, 6]    remove(x, 5)    assert x == [2, 4, 6]    x = []    remove(x, 3)    assert x == []def test_insert():    x = [2, 4]    insert(x, 1)    assert x == [1, 2, 4]    x = [2, 4]    insert(x, 3)    assert x == [2, 3, 4]    x = [2, 4]    insert(x, 5)    assert x == [2, 4, 5]    x = [2, 4]    insert(x, 2)    assert x == [2, 4]    x = [2, 4]    insert(x, 4)    assert x == [2, 4]    x = [2, 3, 4]    insert(x, 3)    assert x == [2, 3, 4]    x = []    insert(x, 3)    assert x == [3]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -