File: select_poll_basic.py

package info (click to toggle)
micropython 1.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 48,944 kB
  • sloc: ansic: 317,850; python: 59,539; xml: 4,241; makefile: 3,530; sh: 1,421; javascript: 744; asm: 681; cpp: 45; exp: 11; pascal: 6
file content (40 lines) | stat: -rw-r--r-- 952 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
try:
    import socket, select, errno

    select.poll  # Raises AttributeError for CPython implementations without poll()
except (ImportError, AttributeError):
    print("SKIP")
    raise SystemExit


poller = select.poll()

s = socket.socket()

poller.register(s)
# https://docs.python.org/3/library/select.html#select.poll.register
# "Registering a file descriptor that’s already registered is not an error,
# and has the same effect as registering the descriptor exactly once."
poller.register(s)
poller.register(s, select.POLLIN | select.POLLOUT)

# 2 args are mandatory unlike register()
try:
    poller.modify(s)
except TypeError:
    print("modify:TypeError")

poller.modify(s, select.POLLIN)

poller.unregister(s)

try:
    poller.modify(s, select.POLLIN)
except OSError as e:
    assert e.errno == errno.ENOENT

# poll after closing the socket, should return POLLNVAL
poller.register(s)
s.close()
p = poller.poll(0)
print(len(p), p[0][-1])