After I did some searching I found the solution. As mentioned here, Gevent's monkey patching does not patch Python's built-in signal.signal
function. In order for my solution to work, the main greenlet must call gevent.signal
instead of signal.signal
in order to set the handler. Also, gevent.signal
expects handler functions which accept no arguments, unlike signal.signal
which expects handler functions which accept 2 arguments. Since I don't care about these arguments anyway, I changed my term handler to look like this:
def _term_handler(*_):
raise TermSignal()
So that the same term handle can be used in Gevent application and in regular Python applications. Although the same handler can be used in both cases, it needs to be set by the appropriate function (signal.signal
or gevent.signal
)