Python select.EPOLLET Examples

The following are 26 code examples of select.EPOLLET(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module select , or try the search function .
Example #1
Source File: conn.py    From CUP with Apache License 2.0 6 votes vote down vote up
def _handle_new_conn(self, newsock, peer):
        self._set_sock_params(newsock)
        self._set_sock_nonblocking(newsock)
        context = sockcontext.CConnContext()
        context.set_sock(newsock)
        context.set_conn_man(self)
        context.set_peerinfo(peer)
        self._epoll.register(
            newsock.fileno(), select.EPOLLIN | select.EPOLLET | select.EPOLLERR
        )
        self._rwlock.acquire_writelock()
        self._fileno2context[newsock.fileno()] = context
        self._peer2context[peer] = context
        self._context2fileno_peer[context] = (newsock.fileno(), peer)
        self._rwlock.release_writelock()
        log.info('a new connection: {0}'.format(peer)) 
Example #2
Source File: conn.py    From CUP with Apache License 2.0 6 votes vote down vote up
def bind(self):
        """
        bind the ip:port
        """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._set_sock_params(sock)
        sock.bind((self._bind_ip, self._bind_port))
        self._set_sock_nonblocking(sock)
        log.info(
            'bind port info:(ip:%s, port:%s)' % (
                self._bind_ip, self._bind_port
            )
        )
        self._epoll.register(
            sock.fileno(),
            select.EPOLLIN | select.EPOLLET | select.EPOLLOUT | select.EPOLLERR
        )
        self._bind_sock = sock 
Example #3
Source File: gpio.py    From python-sysfs-gpio with MIT License 6 votes vote down vote up
def _poll_queue_event(self, events):
        """
        EPoll event callback
        """

        for fd, event in events:
            if not (event & (select.EPOLLPRI | select.EPOLLET)):
                continue

            try:
                values = self._allocated_pins.itervalues()
            except AttributeError:
                values = self._allocated_pins.values()
            for pin in values:
                if pin.fileno() == fd:
                    pin.changed(pin.read()) 
Example #4
Source File: conn.py    From CUP with Apache License 2.0 5 votes vote down vote up
def _epoll_write_params(cls):
        return (select.EPOLLET | select.EPOLLOUT | select.EPOLLERR) 
Example #5
Source File: conn.py    From CUP with Apache License 2.0 5 votes vote down vote up
def _epoll_read_params(cls):
        return (select.EPOLLET | select.EPOLLIN | select.EPOLLERR) 
Example #6
Source File: net09_web_PWB7.py    From python_web_Crawler_DA_ML_DL with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def serve_forever(self):
        """永久运行监听接收连接"""
        while True:
            epoll_list = self.epoll.poll()
            for fd, events in epoll_list:
                if fd == self.tcp_socket.fileno():
                    new_client_socket, new_client_address = self.tcp_socket.accept()
                    print(new_client_address, '向服务器发起了请求')
                    self.connections[new_client_socket.fileno()] = new_client_socket # 存入客户连接事件文件描述符
                    self.addresses[new_client_socket.fileno()] = new_client_address
                    # 向epoll中则侧新socket的可读事件
                    self.epoll.register(new_client_socket.fileno(), select.EPOLLIN | select.EPOLLET)
                elif events == select.EPOLLIN:
                    td = threading.Thread(target=self.handlerequest, args=(fd,))
                    td.start() 
Example #7
Source File: net09_web_PWB7.py    From python_web_Crawler_DA_ML_DL with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, server_address):
        """初始化服务器TCP套接字"""
        self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.tcp_socket.bind(server_address)
        self.tcp_socket.listen(128)
        self.tcp_socket.setblocking(False)  # 将套接字设置为非阻塞模式
        self.epoll = select.epoll()  # 创建一个epoll对象
        self.epoll.register(self.tcp_socket.fileno(), select.EPOLLIN | select.EPOLLET) # 将服务套接字注册
        self.connections = {}
        self.addresses = {} 
Example #8
Source File: net09_web_PWB6.py    From python_web_Crawler_DA_ML_DL with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def serve_forever(self):
        """永久运行监听接收连接"""
        while True:
            epoll_list = self.epoll.poll()
            for fd, events in epoll_list:
                if fd == self.tcp_socket.fileno():
                    new_client_socket, new_client_address = self.tcp_socket.accept()
                    print(new_client_address, '向服务器发起了请求')
                    self.connections[new_client_socket.fileno()] = new_client_socket # 存入客户连接事件文件描述符
                    self.addresses[new_client_socket.fileno()] = new_client_address
                    # 向epoll中则侧新socket的可读事件
                    self.epoll.register(new_client_socket.fileno(), select.EPOLLIN | select.EPOLLET)
                elif events == select.EPOLLIN:
                    self.handlerequest(fd) 
Example #9
Source File: net09_web_PWB6.py    From python_web_Crawler_DA_ML_DL with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, server_address):
        """初始化服务器TCP套接字"""
        self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.tcp_socket.bind(server_address)
        self.tcp_socket.listen(128)
        self.tcp_socket.setblocking(False)  # 将套接字设置为非阻塞模式
        self.epoll = select.epoll()  # 创建一个epoll对象
        self.epoll.register(self.tcp_socket.fileno(), select.EPOLLIN | select.EPOLLET) # 将服务套接字注册
        self.connections = {}
        self.addresses = {} 
Example #10
Source File: poll.py    From vanilla with MIT License 5 votes vote down vote up
def register(self, fd, *masks):
            masks = [self.from_[x] for x in masks] + [
                select.EPOLLET, select.EPOLLERR, select.EPOLLHUP]
            self.q.register(fd, reduce(operator.or_, masks, 0)) 
Example #11
Source File: gpio.py    From python-sysfs-gpio with MIT License 5 votes vote down vote up
def _poll_queue_register_pin(self, pin):
        ''' Pin responds to fileno(), so it's pollable. '''
        self._poll_queue.register(pin, (select.EPOLLPRI | select.EPOLLET)) 
Example #12
Source File: polling.py    From vlcp with Apache License 2.0 5 votes vote down vote up
def __init__(self, options = select.EPOLLET, maxwait = 60):
            '''
            Constructor
            '''
            self.epoll = select.epoll()
            self.defaultoption = options
            self.mask = select.EPOLLIN|select.EPOLLPRI|select.EPOLLOUT|\
                select.EPOLLRDNORM|select.EPOLLRDBAND|select.EPOLLWRNORM|\
                select.EPOLLWRBAND|select.EPOLLONESHOT|select.EPOLLET
            self.socketCounter = 0
            self.maxwait = maxwait
            self.daemons = set()
            self.shouldraise = False 
Example #13
Source File: conn.py    From CUP with Apache License 2.0 5 votes vote down vote up
def _finish_read_callback(self, succ, result):
        context = result
        if context.is_detroying():
            # destroy the context and socket
            context.release_readlock()
            try:
                self.cleanup_error_context(context)
            except KeyError:
                pass
        else:
            self._epoll.modify(
                context.get_sock().fileno(), select.EPOLLIN | select.EPOLLET
            )
            context.release_readlock() 
Example #14
Source File: gpio.py    From respeaker_python_library with Apache License 2.0 5 votes vote down vote up
def _run(self):

        while self._running:
            events = self._poll.poll(EPOLL_TIMEOUT)
            for fd, event in events:
                if not (event & (select.EPOLLPRI | select.EPOLLET)):
                    continue

                self.changed(self.read()) 
Example #15
Source File: mixer.py    From mopidy-alsamixer with Apache License 2.0 5 votes vote down vote up
def run(self):
        poller = select.epoll()
        poller.register(self.fd, self.event_mask | select.EPOLLET)
        while self.running:
            try:
                events = poller.poll(timeout=1)
                if events and self.callback is not None:
                    self.callback()
            except OSError as exc:
                # poller.poll() will raise an IOError because of the
                # interrupted system call when suspending the machine.
                logger.debug(f"Ignored IO error: {exc}") 
Example #16
Source File: test_epoll.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        events = ep.poll(timeout=2.1, maxevents=4)
        self.assertFalse(events)

        client.send("Hello!")
        server.send("world!!!")

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #17
Source File: test_epoll.py    From CTFCrackTools-V2 with GNU General Public License v3.0 4 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)
        self.assertFalse(then - now > 0.01, then - now)

        now = time.time()
        events = ep.poll(timeout=2.1, maxevents=4)
        then = time.time()
        self.assertFalse(events)

        client.send("Hello!")
        server.send("world!!!")

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #18
Source File: test_epoll.py    From CTFCrackTools with GNU General Public License v3.0 4 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)
        self.assertFalse(then - now > 0.01, then - now)

        now = time.time()
        events = ep.poll(timeout=2.1, maxevents=4)
        then = time.time()
        self.assertFalse(events)

        client.send("Hello!")
        server.send("world!!!")

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #19
Source File: test_epoll.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 4 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        events = ep.poll(timeout=2.1, maxevents=4)
        self.assertFalse(events)

        client.send(b"Hello!")
        server.send(b"world!!!")

        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #20
Source File: test_epoll.py    From gcblue with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        events = ep.poll(timeout=2.1, maxevents=4)
        self.assertFalse(events)

        client.send("Hello!")
        server.send("world!!!")

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #21
Source File: test_epoll.py    From ironpython3 with Apache License 2.0 4 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        events = ep.poll(timeout=2.1, maxevents=4)
        self.assertFalse(events)

        client.send(b"Hello!")
        server.send(b"world!!!")

        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #22
Source File: test_epoll.py    From Fluid-Designer with GNU General Public License v3.0 4 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        events = ep.poll(timeout=2.1, maxevents=4)
        self.assertFalse(events)

        client.send(b"Hello!")
        server.send(b"world!!!")

        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.monotonic()
        events = ep.poll(1, 4)
        then = time.monotonic()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #23
Source File: test_epoll.py    From oss-ftp with MIT License 4 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        events = ep.poll(timeout=2.1, maxevents=4)
        self.assertFalse(events)

        client.send("Hello!")
        server.send("world!!!")

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #24
Source File: test_epoll.py    From BinderFilter with MIT License 4 votes vote down vote up
def test_control_and_wait(self):
        client, server = self._connected_pair()

        ep = select.epoll(16)
        ep.register(server.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
        ep.register(client.fileno(),
                   select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.1, then - now)

        events.sort()
        expected = [(client.fileno(), select.EPOLLOUT),
                    (server.fileno(), select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)
        self.assertFalse(then - now > 0.01, then - now)

        now = time.time()
        events = ep.poll(timeout=2.1, maxevents=4)
        then = time.time()
        self.assertFalse(events)

        client.send("Hello!")
        server.send("world!!!")

        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
                    (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
        expected.sort()

        self.assertEqual(events, expected)

        ep.unregister(client.fileno())
        ep.modify(server.fileno(), select.EPOLLOUT)
        now = time.time()
        events = ep.poll(1, 4)
        then = time.time()
        self.assertFalse(then - now > 0.01)

        expected = [(server.fileno(), select.EPOLLOUT)]
        self.assertEqual(events, expected) 
Example #25
Source File: gpio.py    From respeaker_python_library with Apache License 2.0 4 votes vote down vote up
def __init__(self, number, direction=INPUT, callback=None, edge=None, active_low=0):
        """
        @type  number: int
        @param number: The pin number
        @type  direction: int
        @param direction: Pin direction, enumerated by C{Direction}
        @type  callback: callable
        @param callback: Method be called when pin changes state
        @type  edge: int
        @param edge: The edge transition that triggers callback,
                     enumerated by C{Edge}
        @type active_low: int
        @param active_low: Indicator of whether this pin uses inverted
                           logic for HIGH-LOW transitions.
        """
        self._number = number
        self._direction = direction
        self._callback = callback
        self._active_low = active_low

        if not os.path.isfile(self._sysfs_gpio_value_path()):
            with open(SYSFS_EXPORT_PATH, 'w') as export:
                export.write('%d' % number)
        else:
            Logger.debug("SysfsGPIO: Pin %d already exported" % number)

        self._fd = open(self._sysfs_gpio_value_path(), 'r+')

        if callback and not edge:
            raise Exception('You must supply a edge to trigger callback on')

        with open(self._sysfs_gpio_direction_path(), 'w') as fsdir:
            fsdir.write(direction)

        if edge:
            with open(self._sysfs_gpio_edge_path(), 'w') as fsedge:
                fsedge.write(edge)
                self._poll = select.epoll()
                self._poll.register(self, (select.EPOLLPRI | select.EPOLLET))
                self.thread = Thread(target=self._run)
                self.thread.daemon = True
                self._running = True
                self.thread.start()

        if active_low:
            if active_low not in ACTIVE_LOW_MODES:
                raise Exception('You must supply a value for active_low which is either 0 or 1.')
            with open(self._sysfs_gpio_active_low_path(), 'w') as fsactive_low:
                fsactive_low.write(str(active_low)) 
Example #26
Source File: basic03.py    From Python24 with MIT License 4 votes vote down vote up
def testEpollServer():

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    server_socket.setblocking(False)

    server_socket.bind(("", 8888))

    server_socket.listen(128)

    # 创建epoll对象
    epoll = select.epoll()

    # 将服务器的socket添加到epoll的监听列表, fd = file descriptor文件描述符,
    # ET模式: select.EPOLLIN | select.EPOLLET
    epoll.register(server_socket.fileno(), select.EPOLLIN)

    # 保存客户端的socket字典, {fd: socket}
    client_socket_list = {}
    client_address_list = {}

    while True:

        # 死循环中向epoll对象获取监听结果
        epoll_list = epoll.poll()

        # 遍历列表,获取fd和是件
        for fd, events in epoll_list:

            # 服务器socket     accept()
            if fd == server_socket.fileno():
                new_client_socket, new_client_address = server_socket.accept()
                print("收到了来自%s的链接请求" % (str(new_client_address)))

                # 设置客户端socket非阻塞
                new_client_socket.setblocking(False)

                # 添加到epoll监听列表
                epoll.register(new_client_socket.fileno(), epoll.EPOLLIN)

                # 添加到集合
                client_socket_list[new_client_socket.fileno()] = new_client_socket
                client_address_list[new_client_socket.fileno()] = new_client_address

            # 客户端的socket    recv()
            elif events == select.EPOLLIN:
                recv_data = client_socket_list[fd].recv(4096)

                if recv_data:
                    print("收到了来自%s的数据:%s" % (str(client_address_list[fd]), recv_data.decode()))
                else:
                    print("收到了来自%s的断开请求" % (str(client_address_list[fd])))

                    # 从监听列表中移除
                    epoll.unregister(fd)

                    # 从字典中移除
                    del client_socket_list[fd]
                    del client_address_list[fd]