当前位置: 首页>>代码示例>>Python>>正文


Python uselect.poll方法代码示例

本文整理汇总了Python中uselect.poll方法的典型用法代码示例。如果您正苦于以下问题:Python uselect.poll方法的具体用法?Python uselect.poll怎么用?Python uselect.poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在uselect的用法示例。


在下文中一共展示了uselect.poll方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: wait

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def wait(self, delay):
        if DEBUG and __debug__:
            log.debug("poll.wait(%d)", delay)
        # We need one-shot behavior (second arg of 1 to .poll())
        res = self.poller.ipoll(delay, 1)
        #log.debug("poll result: %s", res)
        # Remove "if res" workaround after
        # https://github.com/micropython/micropython/issues/2716 fixed.
        if res:
            for sock, ev in res:
                cb = self.objmap[id(sock)]
                if ev & (select.POLLHUP | select.POLLERR):
                    # These events are returned even if not requested, and
                    # are sticky, i.e. will be returned again and again.
                    # If the caller doesn't do proper error handling and
                    # unregister this sock, we'll busy-loop on it, so we
                    # as well can unregister it now "just in case".
                    self.remove_reader(sock)
                if DEBUG and __debug__:
                    log.debug("Calling IO callback: %r", cb)
                if isinstance(cb, tuple):
                    cb[0](*cb[1])
                else:
                    cb.pend_throw(None)
                    self.call_soon(cb) 
开发者ID:lemariva,项目名称:uPyCam,代码行数:27,代码来源:__init__.py

示例2: poll

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def poll(self, timeout=100):
        if self.use_poll:
            ready = self.poller.poll(timeout)
        else:
            ready = []
            if len(self.targets) > 0:
                (rlist, wlist, xlist) = select.select(
                    self.targets.keys(), [], [], timeout
                )
                ready = [(x, None) for x in rlist]

        for one_ready in ready:
            target = self.targets.get(one_ready[0].fileno(), None)
            dbg("Targets %s" % str(self.targets.keys()))
            if target:
                # dbg("get socket with fileno: %s" % str(one_ready[0].fileno()) +  " len: %s" % len(one_ready) + " selected: %s " % str(target.fileno()) )
                # update time
                target.do_read(one_ready[0]) 
开发者ID:lemariva,项目名称:uPyEcho,代码行数:20,代码来源:main.py

示例3: run

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def run():
    global success
    ok = True
    try:
        while ok:
            res = poller.ipoll(10)
            for sock, ev in res:
                if ev & select.POLLOUT:
                    r = sock.send(b'0123456789\n')
                    print(ev, r)  
                    # On ESP8266 if another task closes the socket the poll object
                    # never triggers. uasyncio expects it to trigger with POLLHUP or
                    # (POLLOUT & POLLERR or POLLOUT & POLLHUP)
                    # If server fails gets OSError on both platforms.
                else:  # But on Unix socket closure produces ev == 32
                    print('Terminating event:', ev)  # What is 32??
                    ok = False
                    break
                await asyncio.sleep(1)
            await asyncio.sleep(0)
    except OSError:
        print('Got OSError')  # Happens on ESP8266 if server fails
    success = True  # Detected socket closure or error by OSError or event 
开发者ID:peterhinch,项目名称:micropython-samples,代码行数:25,代码来源:client_w.py

示例4: read

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def read(self):
        poll_events = self.poll.poll(0)

        if not poll_events:
            return

        # Check the flag for connection hung up
        if poll_events[0][1] & uselect.POLLHUP:
            self.client_close = True

        msg_bytes = None
        try:
            msg_bytes = self.ws.read()
        except OSError:
            self.client_close = True

        # If no bytes => connection closed. See the link below.
        # http://stefan.buettcher.org/cs/conn_closed.html
        if not msg_bytes or self.client_close:
            raise ClientClosedError()

        return msg_bytes 
开发者ID:BetaRavener,项目名称:upy-websocket-server,代码行数:24,代码来源:ws_connection.py

示例5: wait

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def wait(self, delay):
        if DEBUG and __debug__:
            log.debug("poll.wait(%d)", delay)
        # We need one-shot behavior (second arg of 1 to .poll())
        res = self.poller.ipoll(delay, 1)
        # log.debug("poll result: %s", res)
        # Remove "if res" workaround after
        # https://github.com/micropython/micropython/issues/2716 fixed.
        if res:
            for sock, ev in res:
                cb = self.objmap[id(sock)]
                if ev & (select.POLLHUP | select.POLLERR):
                    # These events are returned even if not requested, and
                    # are sticky, i.e. will be returned again and again.
                    # If the caller doesn't do proper error handling and
                    # unregister this sock, we'll busy-loop on it, so we
                    # as well can unregister it now "just in case".
                    self.remove_reader(sock)
                if DEBUG and __debug__:
                    log.debug("Calling IO callback: %r", cb)
                if isinstance(cb, tuple):
                    cb[0](*cb[1])
                else:
                    cb.pend_throw(None)
                    self.call_soon(cb) 
开发者ID:kevinkk525,项目名称:pysmartnode,代码行数:27,代码来源:__init__.py

示例6: run

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def run(self, loop, port=8123):
        addr = socket.getaddrinfo('0.0.0.0', port, 0, socket.SOCK_STREAM)[0][-1]
        s_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # server socket
        s_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s_sock.bind(addr)
        s_sock.listen(5)
        self.socks = [s_sock]  # List of current sockets for .close()
        print('Awaiting connection on port', port)
        poller = select.poll()
        poller.register(s_sock, select.POLLIN)
        client_id = 1  # For user feedback
        while True:
            res = poller.poll(1)  # 1ms block
            if res:  # Only s_sock is polled
                c_sock, _ = s_sock.accept()  # get client socket
                loop.create_task(self.run_client(c_sock, client_id))
                client_id += 1
            await asyncio.sleep_ms(200) 
开发者ID:peterhinch,项目名称:micropython-async,代码行数:20,代码来源:userver.py

示例7: close

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def close(self):
        print("Closing connection.")
        self.poll.unregister(self.socket)
        self.socket.close()
        self.socket = None
        self.ws = None
        if self.close_callback:
            self.close_callback(self) 
开发者ID:BetaRavener,项目名称:upy-websocket-server,代码行数:10,代码来源:ws_connection.py

示例8: __init__

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def __init__(self, runq_len=16, waitq_len=16):
        EventLoop.__init__(self, runq_len, waitq_len)
        self.poller = select.poll()
        self.objmap = {} 
开发者ID:lemariva,项目名称:uPyCam,代码行数:6,代码来源:__init__.py

示例9: __init__

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def __init__(self):
        if "poll" in dir(select):
            self.use_poll = True
            self.poller = select.poll()
        else:
            self.use_poll = False
        self.targets = {} 
开发者ID:lemariva,项目名称:uPyEcho,代码行数:9,代码来源:main.py

示例10: run

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def run(expected, verbose=False, port=8123, timeout=2000):
    addr = socket.getaddrinfo('0.0.0.0', port, 0, socket.SOCK_STREAM)[0][-1]
    s_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # server socket
    s_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s_sock.bind(addr)
    s_sock.listen(len(expected) + 2)
    verbose and print('Awaiting connection.', port)
    poller = select.poll()
    poller.register(s_sock, select.POLLIN)
    to_secs = timeout / 1000  # ms -> secs
    while True:
        res = poller.poll(1)  # 1ms block
        if res:  # Only s_sock is polled
            c_sock, _ = s_sock.accept()  # get client socket
            c_sock.setblocking(False)
            try:
                data = await _readid(c_sock, to_secs)
            except OSError:
                c_sock.close()
            else:
                Connection.go(to_secs, data, verbose, c_sock, s_sock,
                              expected)
        await asyncio.sleep(0.2)


# A Connection persists even if client dies (minimise object creation).
# If client dies Connection is closed: ._close() flags this state by closing its
# socket and setting .sock to None (.status() == False). 
开发者ID:peterhinch,项目名称:micropython-iot,代码行数:30,代码来源:server.py

示例11: __init__

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def __init__(self, addr, s, close_callback):
        self.client_close = False
        self._need_check = False

        self.address = addr
        self.socket = s
        self.ws = websocket(s, True)
        self.poll = uselect.poll()
        self.close_callback = close_callback

        self.socket.setblocking(False)
        self.poll.register(self.socket, uselect.POLLIN) 
开发者ID:BetaRavener,项目名称:upy-websocket-server,代码行数:14,代码来源:ws_connection.py

示例12: _setup_conn

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def _setup_conn(self, port):
        self._listen_s = socket.socket()
        self._listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._listen_poll = uselect.poll()

        ai = socket.getaddrinfo("0.0.0.0", port)
        addr = ai[0][4]

        self._listen_s.bind(addr)
        self._listen_s.listen(1)
        self._listen_poll.register(self._listen_s)
        for i in (network.AP_IF, network.STA_IF):
            iface = network.WLAN(i)
            if iface.active():
                print("WebSocket started on ws://%s:%d" % (iface.ifconfig()[0], port)) 
开发者ID:BetaRavener,项目名称:upy-websocket-server,代码行数:17,代码来源:ws_server.py

示例13: _check_new_connections

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def _check_new_connections(self, accept_handler):
        poll_events = self._listen_poll.poll(0)
        if not poll_events:
            return

        if poll_events[0][1] & uselect.POLLIN:
            accept_handler() 
开发者ID:BetaRavener,项目名称:upy-websocket-server,代码行数:9,代码来源:ws_server.py

示例14: __init__

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def __init__(self, runq_len=16, waitq_len=16, fast_io=0, lp_len=0):
        EventLoop.__init__(self, runq_len, waitq_len, fast_io, lp_len)
        self.poller = select.poll()
        self.rdobjmap = {}
        self.wrobjmap = {}
        self.flags = {}

    # Remove registration of sock for reading or writing. 
开发者ID:peterhinch,项目名称:micropython-async,代码行数:10,代码来源:__init__.py

示例15: read

# 需要导入模块: import uselect [as 别名]
# 或者: from uselect import poll [as 别名]
def read(self, n=-1):
        while True:
            yield IORead(self.polls)
            res = self.ios.read(n)  # Call the device's read method
            if res is not None:
                break
            # This should not happen for real sockets, but can easily
            # happen for stream wrappers (ssl, websockets, etc.)
            #log.warn("Empty read")
        yield IOReadDone(self.polls)  # uasyncio.core calls remove_reader
        # This de-registers device as a read device with poll via
        # PollEventLoop._unregister
        return res  # Next iteration raises StopIteration and returns result 
开发者ID:peterhinch,项目名称:micropython-async,代码行数:15,代码来源:__init__.py


注:本文中的uselect.poll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。