简体   繁体   English

运行多线程的python脚本在键盘中断时退出的程序

[英]Procedure to exit upon keyboard interrupt for python script running multiple threads

I have a script which runs 2 threads infinitely.我有一个无限运行 2 个线程的脚本。 (Each thread is an infinite while loop) Whenever I run it normally, I use ctrl + Z or ctrl + C to stop its execution (depending on the OS). (每个线程都是一个无限的while循环)每当我正常运行它时,我都会使用 ctrl + Z 或 ctrl + C 来停止它的执行(取决于操作系统)。 But ever since I added it to the /etc/rc.local file in Linux, for automatic startup upon boot, I am unable to use these commands to forcefully exit.但是自从我把它添加到Linux中的/etc/rc.local文件后,为了开机自动启动,我无法使用这些命令强制退出。

This has forced me to include something in the python script itself to cleanly exit when I type a certain key.这迫使我在 python 脚本本身中包含一些内容,以便在我键入某个键时干净地退出。 How do I do so?我该怎么做?

The problem is that I'm running a multithreaded application, which runs continuously and does not wait for any user inputs.问题是我正在运行一个多线程应用程序,它连续运行并且不等待任何用户输入。

I added this to the start of a loop in my thread-我将此添加到线程中循环的开头-

ip = raw_input()
if ip == 'quit':
    quit()

But this will NOT work since it blocks for a user input, and stops the script.但这不会起作用,因为它会阻止用户输入并停止脚本。 I don't want the script to be affected at all by this.我不希望脚本受此影响。 I just want it to respond when I want to stop it.我只希望它在我想阻止它时做出响应。 My question is not what command to use (which is explained here- Python exit commands - why so many and when should each be used? ), but how I should use it without affecting the flow of my program.我的问题不是要使用什么命令(这里解释了 - Python 退出命令 - 为什么这么多以及何时应该使用每个命令? ),而是我应该如何在不影响程序流程的情况下使用它。

One way is to have the threads examine a global variable as a part of their loop, and terminate ( break out of the loop and terminate, that is) when the variable is set.一种方法是让线程检查全局变量作为其循环的一部分,并在设置变量时终止(即break循环并终止)。

The main thread can then simply set the variable and join() all existing threads before terminating.然后主线程可以在终止之前简单地设置变量和join()所有现有线程。 You should be aware that if the individual threads are blocked waiting for some event to occur before they next check whether the global variable has been set, then they will hang anyway until that event occurs.您应该知道,如果各个线程在它们下一次检查全局变量是否已设置之前被阻塞等待某个事件发生,那么它们无论如何都会挂起,直到该事件发生。

Keep the code that handles the KeyboardInterrupt and send it an INT signal to stop the program: kill -INT $pid from the shell, where $pid is the process ID (PID) of the program.保留处理KeyboardInterrupt的代码并向其发送一个 INT 信号以停止程序:从 shell 中kill -INT $pid ,其中$pid是程序的进程 ID (PID)。 That's essentially the same as pressing CTRL+C in a shell where the program runs in the foreground.这与在程序在前台运行的 shell 中按 CTRL+C 基本上相同。

Writing the program's PID into a file right after it started, either from within the program itself or from the code which started it asynchronously, makes it easier to send a signal later, without the need to search for the process in the process list.在程序启动后立即将程序的 PID 写入文件,无论是从程序本身还是从异步启动它的代码,可以更轻松地稍后发送信号,而无需在进程列表中搜索进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM