Klaus Demo bjoern / daf5949
Periodically call PyErr_CheckSignals() Jonas Haag 7 months ago
4 changed file(s) with 58 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
2222
2323 ifneq ($(WANT_SIGINT_HANDLING), no)
2424 FEATURES += -D WANT_SIGINT_HANDLING
25 endif
26
27 ifneq ($(WANT_SIGNAL_HANDLING), no)
28 FEATURES += -D WANT_SIGNAL_HANDLING
29 endif
30
31 ifndef SIGNAL_CHECK_INTERVAL
32 FEATURES += -D SIGNAL_CHECK_INTERVAL=0.1
2533 endif
2634
2735 all: prepare-build $(objects) _bjoernmodule
4949 #if WANT_SIGINT_HANDLING
5050 typedef void ev_signal_callback(struct ev_loop*, ev_signal*, const int);
5151 static ev_signal_callback ev_signal_on_sigint;
52 #endif
53
54 #if WANT_SIGINT_HANDLING
55 typedef void ev_timer_callback(struct ev_loop*, ev_timer*, const int);
56 static ev_timer_callback ev_timer_ontick;
57 ev_timer timeout_watcher;
5258 #endif
5359
5460 static ev_io_callback ev_io_on_request;
7480 ev_io_start(mainloop, &thread_info.accept_watcher);
7581
7682 #if WANT_SIGINT_HANDLING
77 ev_signal signal_watcher;
78 ev_signal_init(&signal_watcher, ev_signal_on_sigint, SIGINT);
79 ev_signal_start(mainloop, &signal_watcher);
83 ev_signal sigint_watcher;
84 ev_signal_init(&sigint_watcher, ev_signal_on_sigint, SIGINT);
85 ev_signal_start(mainloop, &sigint_watcher);
86 #endif
87
88 #ifdef WANT_SIGNAL_HANDLING
89 ev_timer_init(&timeout_watcher, ev_timer_ontick, 0., SIGNAL_CHECK_INTERVAL);
90 ev_timer_start(mainloop, &timeout_watcher);
91 ev_set_priority(&timeout_watcher, EV_MINPRI);
8092 #endif
8193
8294 /* This is the program main loop */
105117
106118 ev_io_stop(mainloop, &((ThreadInfo*)ev_userdata(mainloop))->accept_watcher);
107119 ev_signal_stop(mainloop, watcher);
120 #ifdef WANT_SIGNAL_HANDLING
121 ev_timer_stop(mainloop, &timeout_watcher);
122 #endif
123 }
124 #endif
125
126 #if WANT_SIGNAL_HANDLING
127 static void
128 ev_timer_ontick(struct ev_loop* mainloop, ev_timer* watcher, const int events)
129 {
130 GIL_LOCK(0);
131 PyErr_CheckSignals();
132 GIL_UNLOCK(0);
108133 }
109134 #endif
110135
1010 libraries = ['ev'],
1111 include_dirs = ['http-parser', '/usr/include/libev'],
1212 define_macros = [('WANT_SENDFILE', '1'),
13 ('WANT_SIGINT_HANDLING', '1')],
13 ('WANT_SIGINT_HANDLING', '1'),
14 ('WANT_SIGNAL_HANDLING', '1'),
15 ('SIGNAL_CHECK_INTERVAL', '0.1')],
1416 extra_compile_args = ['-std=c99', '-fno-strict-aliasing', '-fcommon',
1517 '-fPIC', '-Wall', '-Wextra', '-Wno-unused-parameter',
1618 '-Wno-missing-field-initializers', '-g']
0 import signal
1
2 n = 0
3
4 def app(e, s):
5 s('200 ok', [])
6 return b'%d times' % n
7
8
9 def inc_counter(*args):
10 global n
11 n += 1
12 print('Increased counter to',n)
13
14
15 signal.signal(signal.SIGTERM, inc_counter)
16
17 import bjoern
18 bjoern.run(app, '0.0.0.0', 8080)