Python 3.x 接收到的信号退出程序,但不调用信号处理程序 问题

Python 3.x 接收到的信号退出程序,但不调用信号处理程序 问题,python-3.x,multithreading,signals,Python 3.x,Multithreading,Signals,程序在主线程内运行后,我发送signal.SIGINT信号中断程序并导致退出。程序关闭,但是由于某种原因没有调用信号处理程序,我需要调用信号处理程序来运行一些清理代码 背景信息 目前我正在运行: -Windows 10 -Python 3.7.3 x64 我尝试过从内置信号模块发送多个不同的信号,例如SIGINT、SIGTERM和CTRL_C_事件。在所有情况下,程序流都会中断,程序会退出,但不会调用信号处理程序 我知道所有信号都只由主函数接收,所以这是我在代码中尝试遵守的 代码 其中: cli

程序在主线程内运行后,我发送signal.SIGINT信号中断程序并导致退出。程序关闭,但是由于某种原因没有调用信号处理程序,我需要调用信号处理程序来运行一些清理代码

背景信息 目前我正在运行:
-Windows 10
-Python 3.7.3 x64

我尝试过从内置信号模块发送多个不同的信号,例如SIGINT、SIGTERM和CTRL_C_事件。在所有情况下,程序流都会中断,程序会退出,但不会调用信号处理程序

我知道所有信号都只由主函数接收,所以这是我在代码中尝试遵守的

代码 其中:
client=重写中的不协调客户端对象
init_sanic()=初始化sanic web框架

类强制关闭(异常):
"""
要引发的自定义异常
"""
通过
def信号处理器(信号,帧):
打印(f'接收的信号为{signal}')
提升强制关闭
如果(uuuu name_uuuuuu=='uuuuuu main_uuuuu'):
尝试:
signal.signal(signal.SIGINT,信号处理器)
client=DiscordBot()
bot\u thread=thread(target=lambda:client.run(setup\u pars['client\u TOKEN']))
api_thread=thread(target=lambda:init_sanic())
线程=[api_线程,bot_线程]
对于线程中的x:
x、 开始()
尽管如此:
时间。睡眠(100)
除了(强制关机、系统退出):
打印('执行关闭操作')
输出:

[2019-07-31 19:49:18 +0100] [11808] [DEBUG] 

                 Sanic
         Build Fast. Run Fast.


[2019-07-31 19:49:18 +0100] [11808] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [INFO] Starting worker [11808]
Discord Bot Loaded
import os
import signal

os.kill(pid, signal.SIGINT)
Process finished with exit code 2
用于发送信号的代码:

[2019-07-31 19:49:18 +0100] [11808] [DEBUG] 

                 Sanic
         Build Fast. Run Fast.


[2019-07-31 19:49:18 +0100] [11808] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [INFO] Starting worker [11808]
Discord Bot Loaded
import os
import signal

os.kill(pid, signal.SIGINT)
Process finished with exit code 2
最终输出:

[2019-07-31 19:49:18 +0100] [11808] [DEBUG] 

                 Sanic
         Build Fast. Run Fast.


[2019-07-31 19:49:18 +0100] [11808] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [INFO] Starting worker [11808]
Discord Bot Loaded
import os
import signal

os.kill(pid, signal.SIGINT)
Process finished with exit code 2
预期产出 当代码按预期退出时,输出为:

[2019-07-31 19:49:18 +0100] [11808] [DEBUG] 

                 Sanic
         Build Fast. Run Fast.


[2019-07-31 19:49:18 +0100] [11808] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [INFO] Starting worker [11808]
Discord Bot Loaded

Process finished with exit code 2
鉴于预期最终产出为:

[2019-07-31 19:49:18 +0100] [11808] [DEBUG] 

                 Sanic
         Build Fast. Run Fast.


[2019-07-31 19:49:18 +0100] [11808] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-07-31 19:49:18 +0100] [11808] [INFO] Starting worker [11808]
Discord Bot Loaded
DO SHUTDOWN ACTIONS

Process finished with exit code 2
谢谢你的帮助