python中keyboardinterrupt_Cython,Python和KeyboardInterrupt被忽略了

有没有办法根据嵌入在Cython扩展中的循环中断(Ctrl C)Python脚本?

我有以下python脚本:

def main():

# Intantiate simulator

sim = PySimulator()

sim.Run()

if __name__ == "__main__":

# Try to deal with Ctrl+C to abort the running simulation in terminal

# (Doesn't work...)

try:

sys.exit(main())

except (KeyboardInterrupt, SystemExit):

print '

! Received keyboard interrupt, quitting threads.

'

这将运行一个循环,它是C Cython扩展的一部分.

然后,在按下Ctrl C的同时,抛出KeyboardInterrupt但忽略,程序一直持续到模拟结束.

我找到的工作是通过捕获SIGINT信号来处理扩展内的异常:

#include

#include

static void handler(int sig)

{

// Catch exceptions

switch(sig)

{

case SIGABRT:

fputs("Caught SIGABRT: usually caused by an abort() or assert()

", stderr);

break;

case SIGFPE:

fputs("Caught SIGFPE: arithmetic exception, such as divide by zero

",

stderr);

break;

case SIGILL:

fputs("Caught SIGILL: illegal instruction

", stderr);

break;

case SIGINT:

fputs("Caught SIGINT: interactive attention signal, probably a ctrl+c

",

stderr);

break;

case SIGSEGV:

fputs("Caught SIGSEGV: segfault

", stderr);

break;

case SIGTERM:

default:

fputs("Caught SIGTERM: a termination request was sent to the program

",

stderr);

break;

}

exit(sig);

}

然后 :

signal(SIGABRT, handler);

signal(SIGFPE, handler);

signal(SIGILL, handler);

signal(SIGINT, handler);

signal(SIGSEGV, handler);

signal(SIGTERM, handler);

我不能用Python,或者至少从Cython中做这项工作吗?当我即将在Windows / MinGW下移植我的扩展时,我希望能有一些不那么具体的Linux.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值