ominfra 0.0.0.dev129__py3-none-any.whl → 0.0.0.dev131__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,240 +0,0 @@
1
- # ruff: noqa: UP006 UP007
2
- import abc
3
- import errno
4
- import select
5
- import sys
6
- import typing as ta
7
-
8
- from omlish.lite.logs import log
9
-
10
- from .setup import DaemonizeListener
11
- from .utils.ostypes import Fd
12
-
13
-
14
- class Poller(DaemonizeListener, abc.ABC):
15
- def __init__(self) -> None:
16
- super().__init__()
17
-
18
- @abc.abstractmethod
19
- def register_readable(self, fd: Fd) -> None:
20
- raise NotImplementedError
21
-
22
- @abc.abstractmethod
23
- def register_writable(self, fd: Fd) -> None:
24
- raise NotImplementedError
25
-
26
- @abc.abstractmethod
27
- def unregister_readable(self, fd: Fd) -> None:
28
- raise NotImplementedError
29
-
30
- @abc.abstractmethod
31
- def unregister_writable(self, fd: Fd) -> None:
32
- raise NotImplementedError
33
-
34
- @abc.abstractmethod
35
- def poll(self, timeout: ta.Optional[float]) -> ta.Tuple[ta.List[Fd], ta.List[Fd]]:
36
- raise NotImplementedError
37
-
38
- def before_daemonize(self) -> None: # noqa
39
- pass
40
-
41
- def after_daemonize(self) -> None: # noqa
42
- pass
43
-
44
- def close(self) -> None: # noqa
45
- pass
46
-
47
-
48
- class SelectPoller(Poller):
49
- def __init__(self) -> None:
50
- super().__init__()
51
-
52
- self._readable: ta.Set[Fd] = set()
53
- self._writable: ta.Set[Fd] = set()
54
-
55
- def register_readable(self, fd: Fd) -> None:
56
- self._readable.add(fd)
57
-
58
- def register_writable(self, fd: Fd) -> None:
59
- self._writable.add(fd)
60
-
61
- def unregister_readable(self, fd: Fd) -> None:
62
- self._readable.discard(fd)
63
-
64
- def unregister_writable(self, fd: Fd) -> None:
65
- self._writable.discard(fd)
66
-
67
- def unregister_all(self) -> None:
68
- self._readable.clear()
69
- self._writable.clear()
70
-
71
- def poll(self, timeout: ta.Optional[float]) -> ta.Tuple[ta.List[Fd], ta.List[Fd]]:
72
- try:
73
- r, w, x = select.select(
74
- self._readable,
75
- self._writable,
76
- [], timeout,
77
- )
78
- except OSError as exc:
79
- if exc.args[0] == errno.EINTR:
80
- log.debug('EINTR encountered in poll')
81
- return [], []
82
- if exc.args[0] == errno.EBADF:
83
- log.debug('EBADF encountered in poll')
84
- self.unregister_all()
85
- return [], []
86
- raise
87
- return r, w
88
-
89
-
90
- class PollPoller(Poller):
91
- _READ = select.POLLIN | select.POLLPRI | select.POLLHUP
92
- _WRITE = select.POLLOUT
93
-
94
- def __init__(self) -> None:
95
- super().__init__()
96
-
97
- self._poller = select.poll()
98
- self._readable: set[Fd] = set()
99
- self._writable: set[Fd] = set()
100
-
101
- def register_readable(self, fd: Fd) -> None:
102
- self._poller.register(fd, self._READ)
103
- self._readable.add(fd)
104
-
105
- def register_writable(self, fd: Fd) -> None:
106
- self._poller.register(fd, self._WRITE)
107
- self._writable.add(fd)
108
-
109
- def unregister_readable(self, fd: Fd) -> None:
110
- self._readable.discard(fd)
111
- self._poller.unregister(fd)
112
- if fd in self._writable:
113
- self._poller.register(fd, self._WRITE)
114
-
115
- def unregister_writable(self, fd: Fd) -> None:
116
- self._writable.discard(fd)
117
- self._poller.unregister(fd)
118
- if fd in self._readable:
119
- self._poller.register(fd, self._READ)
120
-
121
- def poll(self, timeout: ta.Optional[float]) -> ta.Tuple[ta.List[Fd], ta.List[Fd]]:
122
- fds = self._poll_fds(timeout) # type: ignore
123
- readable, writable = [], []
124
- for fd, eventmask in fds:
125
- if self._ignore_invalid(fd, eventmask):
126
- continue
127
- if eventmask & self._READ:
128
- readable.append(fd)
129
- if eventmask & self._WRITE:
130
- writable.append(fd)
131
- return readable, writable
132
-
133
- def _poll_fds(self, timeout: float) -> ta.List[ta.Tuple[Fd, Fd]]:
134
- try:
135
- return self._poller.poll(timeout * 1000) # type: ignore
136
- except OSError as exc:
137
- if exc.args[0] == errno.EINTR:
138
- log.debug('EINTR encountered in poll')
139
- return []
140
- raise
141
-
142
- def _ignore_invalid(self, fd: Fd, eventmask: int) -> bool:
143
- if eventmask & select.POLLNVAL:
144
- # POLLNVAL means `fd` value is invalid, not open. When a process quits it's `fd`s are closed so there is no
145
- # more reason to keep this `fd` registered If the process restarts it's `fd`s are registered again.
146
- self._poller.unregister(fd)
147
- self._readable.discard(fd)
148
- self._writable.discard(fd)
149
- return True
150
- return False
151
-
152
-
153
- if sys.platform == 'darwin' or sys.platform.startswith('freebsd'):
154
- class KqueuePoller(Poller):
155
- max_events = 1000
156
-
157
- def __init__(self) -> None:
158
- super().__init__()
159
-
160
- self._kqueue: ta.Optional[ta.Any] = select.kqueue()
161
- self._readable: set[Fd] = set()
162
- self._writable: set[Fd] = set()
163
-
164
- def register_readable(self, fd: Fd) -> None:
165
- self._readable.add(fd)
166
- kevent = select.kevent(fd, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_ADD)
167
- self._kqueue_control(fd, kevent)
168
-
169
- def register_writable(self, fd: Fd) -> None:
170
- self._writable.add(fd)
171
- kevent = select.kevent(fd, filter=select.KQ_FILTER_WRITE, flags=select.KQ_EV_ADD)
172
- self._kqueue_control(fd, kevent)
173
-
174
- def unregister_readable(self, fd: Fd) -> None:
175
- kevent = select.kevent(fd, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_DELETE)
176
- self._readable.discard(fd)
177
- self._kqueue_control(fd, kevent)
178
-
179
- def unregister_writable(self, fd: Fd) -> None:
180
- kevent = select.kevent(fd, filter=select.KQ_FILTER_WRITE, flags=select.KQ_EV_DELETE)
181
- self._writable.discard(fd)
182
- self._kqueue_control(fd, kevent)
183
-
184
- def _kqueue_control(self, fd: Fd, kevent: 'select.kevent') -> None:
185
- try:
186
- self._kqueue.control([kevent], 0) # type: ignore
187
- except OSError as error:
188
- if error.errno == errno.EBADF:
189
- log.debug('EBADF encountered in kqueue. Invalid file descriptor %s', fd)
190
- else:
191
- raise
192
-
193
- def poll(self, timeout: ta.Optional[float]) -> ta.Tuple[ta.List[Fd], ta.List[Fd]]:
194
- readable, writable = [], [] # type: ignore
195
-
196
- try:
197
- kevents = self._kqueue.control(None, self.max_events, timeout) # type: ignore
198
- except OSError as error:
199
- if error.errno == errno.EINTR:
200
- log.debug('EINTR encountered in poll')
201
- return readable, writable
202
- raise
203
-
204
- for kevent in kevents:
205
- if kevent.filter == select.KQ_FILTER_READ:
206
- readable.append(kevent.ident)
207
- if kevent.filter == select.KQ_FILTER_WRITE:
208
- writable.append(kevent.ident)
209
-
210
- return readable, writable
211
-
212
- def before_daemonize(self) -> None:
213
- self.close()
214
-
215
- def after_daemonize(self) -> None:
216
- self._kqueue = select.kqueue()
217
- for fd in self._readable:
218
- self.register_readable(fd)
219
- for fd in self._writable:
220
- self.register_writable(fd)
221
-
222
- def close(self) -> None:
223
- self._kqueue.close() # type: ignore
224
- self._kqueue = None
225
-
226
- else:
227
- KqueuePoller = None
228
-
229
-
230
- def get_poller_impl() -> ta.Type[Poller]:
231
- if (
232
- (sys.platform == 'darwin' or sys.platform.startswith('freebsd')) and
233
- hasattr(select, 'kqueue') and
234
- KqueuePoller is not None
235
- ):
236
- return KqueuePoller
237
- elif hasattr(select, 'poll'):
238
- return PollPoller
239
- else:
240
- return SelectPoller