-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsimple.py
345 lines (276 loc) · 11.5 KB
/
simple.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# =============================================================================
# DelayedKeyboardInterrupt implementation.
# This code can be moved into separate python package.
# =============================================================================
import os
import signal
__all__ = [
'SIGNAL_TRANSLATION_MAP',
]
SIGNAL_TRANSLATION_MAP = {
signal.SIGINT: 'SIGINT',
signal.SIGTERM: 'SIGTERM',
}
class DelayedKeyboardInterrupt:
def __init__(self, propagate_to_forked_processes=None):
"""
Constructs a context manager that suppresses SIGINT & SIGTERM signal handlers
for a block of code.
The signal handlers are called on exit from the block.
Inspired by: https://stackoverflow.com/a/21919644
:param propagate_to_forked_processes: This parameter controls behavior of this context manager
in forked processes.
If True, this context manager behaves the same way in forked processes as in parent process.
If False, signals received in forked processes are handled by the original signal handler.
If None, signals received in forked processes are ignored (default).
"""
self._pid = os.getpid()
self._propagate_to_forked_processes = propagate_to_forked_processes
self._sig = None
self._frame = None
self._old_signal_handler_map = None
def __enter__(self):
self._old_signal_handler_map = {
sig: signal.signal(sig, self._handler)
for sig, _ in SIGNAL_TRANSLATION_MAP.items()
}
def __exit__(self, exc_type, exc_val, exc_tb):
for sig, handler in self._old_signal_handler_map.items():
signal.signal(sig, handler)
if self._sig is None:
return
self._old_signal_handler_map[self._sig](self._sig, self._frame)
def _handler(self, sig, frame):
self._sig = sig
self._frame = frame
#
# Protection against fork.
#
if os.getpid() != self._pid:
if self._propagate_to_forked_processes is False:
print(f'!!! DelayedKeyboardInterrupt._handler: {SIGNAL_TRANSLATION_MAP[sig]} received; '
f'PID mismatch: {os.getpid()=}, {self._pid=}, calling original handler')
self._old_signal_handler_map[self._sig](self._sig, self._frame)
elif self._propagate_to_forked_processes is None:
print(f'!!! DelayedKeyboardInterrupt._handler: {SIGNAL_TRANSLATION_MAP[sig]} received; '
f'PID mismatch: {os.getpid()=}, ignoring the signal')
return
# elif self._propagate_to_forked_processes is True:
# ... passthrough
print(f'!!! DelayedKeyboardInterrupt._handler: {SIGNAL_TRANSLATION_MAP[sig]} received; delaying KeyboardInterrupt')
# =============================================================================
# Main script code.
# =============================================================================
import asyncio
import signal
from concurrent.futures import Executor, ThreadPoolExecutor
from typing import Dict, Optional, Any
class AsyncService1:
"""
Dummy service that does nothing.
"""
def __init__(self):
pass
async def start(self):
print(f'AsyncService1: starting')
await asyncio.sleep(1)
print(f'AsyncService1: started')
async def stop(self):
print(f'AsyncService1: stopping')
await asyncio.sleep(1)
print(f'AsyncService1: stopped')
class AsyncService2:
"""
Dummy service that does nothing.
"""
def __init__(self):
pass
async def start(self):
print(f'AsyncService2: starting')
await asyncio.sleep(1)
print(f'AsyncService2: started')
async def stop(self):
print(f'AsyncService2: stopping')
await asyncio.sleep(1)
print(f'AsyncService2: stopped')
class AsyncApplication:
def __init__(self):
self._loop = None # type: Optional[asyncio.AbstractEventLoop]
self._wait_event = None # type: Optional[asyncio.Event]
self._wait_task = None # type: Optional[asyncio.Task]
self._service1 = None # type: Optional[AsyncService1]
self._service2 = None # type: Optional[AsyncService2]
def run(self):
self._loop = asyncio.new_event_loop()
try:
#
# Shield _start() from termination.
#
try:
with DelayedKeyboardInterrupt():
self._start()
#
# If there was an attempt to terminate the application,
# the KeyboardInterrupt is raised AFTER the _start() finishes
# its job.
#
# In that case, the KeyboardInterrupt is re-raised and caught in
# exception handler below and _stop() is called to clean all resources.
#
# Note that it might be generally unsafe to call stop() methods
# on objects that are not started properly.
# This is the main reason why the whole execution of _start()
# is shielded.
#
except KeyboardInterrupt:
print(f'!!! AsyncApplication.run: got KeyboardInterrupt during start')
raise
#
# Application is started now and is running.
# Wait for a termination event infinitelly.
#
print(f'AsyncApplication.run: entering wait loop')
self._wait()
print(f'AsyncApplication.run: exiting wait loop')
except KeyboardInterrupt:
#
# The _stop() is also shielded from termination.
#
try:
with DelayedKeyboardInterrupt():
self._stop()
except KeyboardInterrupt:
print(f'!!! AsyncApplication.run: got KeyboardInterrupt during stop')
async def _astart(self):
self._service1 = AsyncService1()
self._service2 = AsyncService2()
await self._service1.start()
await self._service2.start()
async def _astop(self):
await self._service2.stop()
await self._service1.stop()
async def _await(self):
self._wait_event = asyncio.Event()
self._wait_task = asyncio.create_task(self._wait_event.wait())
await self._wait_task
def _start(self):
self._loop.run_until_complete(self._astart())
def _stop(self):
self._loop.run_until_complete(self._astop())
#
# Because we want clean exit, we patiently wait for completion
# of the _wait_task (otherwise this task might get cancelled
# in the _cancel_all_tasks() method - which wouldn't be a problem,
# but it would be dirty).
#
# The _wait_event & _wait_task might not exist if the application
# has been terminated before calling _wait(), therefore we have to
# carefully check for their presence.
#
if self._wait_event:
self._wait_event.set()
if self._wait_task:
self._loop.run_until_complete(self._wait_task)
#
# Before the loop is finalized, we setup an exception handler that
# suppresses several nasty exceptions.
#
# ConnectionResetError
# --------------------
# This exception is sometimes raised on Windows, possibly because of a bug in Python.
#
# ref: https://bugs.python.org/issue39010
#
# When this exception is raised, the context looks like this:
# context = {
# 'message': 'Error on reading from the event loop self pipe',
# 'exception': ConnectionResetError(
# 22, 'The I/O operation has been aborted because of either a thread exit or an application request',
# None, 995, None
# ),
# 'loop': <ProactorEventLoop running=True closed=False debug=False>
# }
#
# OSError
# -------
# This exception is sometimes raised on Windows - usually when application is
# interrupted early after start.
#
# When this exception is raised, the context looks like this:
# context = {
# 'message': 'Cancelling an overlapped future failed',
# 'exception': OSError(9, 'The handle is invalid', None, 6, None),
# 'future': <_OverlappedFuture pending overlapped=<pending, 0x1d8937601f0>
# cb=[BaseProactorEventLoop._loop_self_reading()]>,
# }
#
def __loop_exception_handler(loop, context: Dict[str, Any]):
if type(context['exception']) == ConnectionResetError:
print(f'!!! AsyncApplication._stop.__loop_exception_handler: suppressing ConnectionResetError')
elif type(context['exception']) == OSError:
print(f'!!! AsyncApplication._stop.__loop_exception_handler: suppressing OSError')
else:
print(f'!!! AsyncApplication._stop.__loop_exception_handler: unhandled exception: {context}')
self._loop.set_exception_handler(__loop_exception_handler)
try:
#
# Cancel all remaining uncompleted tasks.
# We should strive to not make any, but mistakes happen and laziness
# is also a thing.
#
# Generally speaking, cancelling tasks shouldn't do any harm (unless
# they do...).
#
self._cancel_all_tasks()
#
# Shutdown all active asynchronous generators.
#
self._loop.run_until_complete(self._loop.shutdown_asyncgens())
finally:
#
# ... and close the loop.
#
print(f'AsyncApplication._stop: closing event loop')
self._loop.close()
def _wait(self):
self._loop.run_until_complete(self._await())
def _cancel_all_tasks(self):
"""
Cancel all tasks in the loop.
This method injects an asyncio.CancelledError exception
into all tasks and lets them handle it.
Note that after cancellation, the event loop is executed again and
waits for all tasks to complete the cancellation. This means that
if some task contains code similar to this:
>>> except asyncio.CancelledError:
>>> await asyncio.Event().wait()
... then the loop doesn't ever finish.
"""
#
# Code kindly borrowed from asyncio.run().
#
to_cancel = asyncio.tasks.all_tasks(self._loop)
print(f'AsyncApplication._cancel_all_tasks: cancelling {len(to_cancel)} tasks ...')
if not to_cancel:
return
for task in to_cancel:
task.cancel()
self._loop.run_until_complete(
asyncio.tasks.gather(*to_cancel, loop=self._loop, return_exceptions=True)
)
for task in to_cancel:
if task.cancelled():
continue
if task.exception() is not None:
self._loop.call_exception_handler({
'message': 'unhandled exception during Application.run() shutdown',
'exception': task.exception(),
'task': task,
})
def main():
print(f'main: begin')
app = AsyncApplication()
app.run()
print(f'main: end')
if __name__ == '__main__':
main()