Interrupt the Python consumer when receiving SIGINT
@@ -30,10 +30,27 @@ void Consumer_unsubscribe(Consumer& consumer) {
Message Consumer_receive(Consumer& consumer) {
Message msg;
Result res;
+
+ while (true) {
Py_BEGIN_ALLOW_THREADS
- res = consumer.receive(msg);
+ // Use 100ms timeout to periodically check whether the
+ // interpreter was interrupted
+ res = consumer.receive(msg, 100);
Py_END_ALLOW_THREADS
+ if (res != ResultTimeout) {
+ // In case of timeout we keep calling receive() to simulate a
+ // blocking call until a message is available, while breaking
+ // every once in a while to check the Python signal status
+ break;
+ }
+
+ if (PyErr_CheckSignals() == -1) {
+ PyErr_SetInterrupt();
+ return msg;
+ }
+ }
+
CHECK_RESULT(res);
return msg;
}
