<p>I'm currently running a flask server with uwsgi. It is a single process and multi threaded server. I'd like to perform some activities on the pod when k8s sends a SIGTERM or SIGINT signal. However when I try to do the following inside my class,</p>
<pre><code>def __register_signal_handlers(self):
    signal.signal(signal.SIGINT, self.__exit_gracefully)
    signal.signal(signal.SIGTERM, self.__exit_gracefully)
</code></pre>
<p>It raises <code>ValueError: Signal must be registered on the main thread</code>
I also checked uwsgi signal register and they don't mention anything about SIGTERM or SIGINT.</p>
<p>How do I capture the above mentioned signal so that I can perform some activities when a pod is terminated by k8s?</p>

Vaibhav
<p>The question and answer below should be helpful</p>
<p>The issue is as the error describes, you have to have signal registered on the main thread, if you do it within flask its on the workers.  So you need to register signal in your process that starts flask.  Then you set up sometime of monitor or thread manager, and when the signal is called, you use that to handle your threads.</p>
<p>If you need them to do additional activity before exiting, it's possible with this setup, just maybe a bit more complicated.  You just need to have some way to then signal to the thread its down from there, which there a multiple ways to do it.</p>
<p><a href="https://stackoverflow.com/questions/68709020/python-signal-only-works-in-main-thread-of-the-main-interpreter-in-flask-with">python &quot;signal only works in main thread of the main interpreter&quot; in flask with subprocess</a></p>
