+-

在阅读之前请不要认为它是重复的.有很多关于多线程和键盘中断的问题,但我没有找到任何考虑os.system,它看起来很重要.
我有一个python脚本,它在工作线程中进行一些外部调用.
我希望它退出,如果我按ctrl c但它看起来像主线程忽略它.
像这样的东西:
from threading import Thread import sys import os def run(i): while True: os.system("sleep 10") print i def main(): threads=[] try: for i in range(0, 3): threads.append(Thread(target=run, args=(i,))) threads[i].daemon=True threads[i].start() for i in range(0, 3): while True: threads[i].join(10) if not threads[i].isAlive(): break except(KeyboardInterrupt, SystemExit): sys.exit("Interrupted by ctrl+c\n") if __name__ == '__main__': main()令人惊讶的是,如果我将os.system(“sleep 10”)更改为time.sleep(10),它可以正常工作.
最佳答案
我不确定你使用的操作系统和shell是什么.我用zsh描述了Mac OS X和Linux(bash / sh应该类似).
当您按Ctrl C时,所有程序都在当前终端receive the signal SIGINT的前台运行.在您的情况下,它是您的主要python进程以及os.system生成的所有进程.
os.system生成的进程然后终止它们的执行.通常当python脚本收到SIGINT时,它会引发KeyboardInterrupt异常,但由于os.system(),你的主进程会忽略SIGINT. Python os.system()calls the Standard C function system(),它使调用进程忽略SIGINT(man Linux/man Mac OS X).
所以你的python线程都没有收到SIGINT,它只是获得它的子进程.
当你删除os.system()调用时,你的python进程停止忽略SIGINT,你得到KeyboardInterrupt.
您可以用subprocess.call([“sleep”,“10”])替换os.system(“sleep 10”). subprocess.call()不会使您的进程忽略SIGINT.
点击查看更多相关文章
转载注明原文:使用os.system()调用的Python线程.主线程不会在ctrl c上退出 - 乐贴网