[英]How to stop/terminate a python script from running?
I wrote a program in IDLE to tokenize text files and it starts to tokeniza 349 text files?我在 IDLE 中编写了一个程序来标记文本文件,它开始标记 349 个文本文件? How can I stop it?
我怎样才能阻止它? How can I stop a running Python program?
如何停止正在运行的 Python 程序?
You can also do it if you use the exit()
function in your code.如果在代码中使用
exit()
函数,也可以这样做。 More ideally, you can do sys.exit()
.更理想的是,您可以执行
sys.exit()
。 sys.exit()
which might terminate Python even if you are running things in parallel through the multiprocessing
package. sys.exit()
即使您通过multiprocessing
包并行运行,它也可能终止 Python。
Note: In order to use the sys.exit()
, you must import it: import sys
注意:为了使用
sys.exit()
,您必须导入它: import sys
要停止您的程序,只需按Control + C 。
If your program is running at an interactive console, pressing CTRL + C will raise a KeyboardInterrupt
exception on the main thread.如果您的程序在交互式控制台上运行,按CTRL + C将在主线程上引发
KeyboardInterrupt
异常。
If your Python program doesn't catch it, the KeyboardInterrupt
will cause Python to exit.如果您的 Python 程序没有捕捉到它,
KeyboardInterrupt
将导致 Python 退出。 However, an except KeyboardInterrupt:
block, or something like a bare except:
, will prevent this mechanism from actually stopping the script from running.但是,一个
except KeyboardInterrupt:
块,或类似一个空的except:
,将阻止此机制实际停止脚本运行。
Sometimes if KeyboardInterrupt
is not working you can send a SIGBREAK
signal instead;有时,如果
KeyboardInterrupt
不起作用,您可以改为发送SIGBREAK
信号; on Windows, CTRL + Pause/Break may be handled by the interpreter without generating a catchable KeyboardInterrupt
exception.在 Windows 上, CTRL + Pause/Break可以由解释器处理,而不会产生可捕获的
KeyboardInterrupt
异常。
However, these mechanisms mainly only work if the Python interpreter is running and responding to operating system events.但是,这些机制主要仅在 Python 解释器正在运行并响应操作系统事件时才起作用。 If the Python interpreter is not responding for some reason, the most effective way is to terminate the entire operating system process that is running the interpreter.
如果 Python 解释器由于某种原因没有响应,最有效的方法是终止运行解释器的整个操作系统进程。 The mechanism for this varies by operating system.
此机制因操作系统而异。
In a Unix-style shell environment, you can press CTRL + Z to suspend whatever process is currently controlling the console.在 Unix 风格的 shell 环境中,您可以按CTRL + Z来挂起当前控制控制台的任何进程。 Once you get the shell prompt back, you can use
jobs
to list suspended jobs, and you can kill the first suspended job with kill %1
.返回 shell 提示后,您可以使用
jobs
列出挂起的作业,并且可以使用kill %1
杀死第一个挂起的作业。 (If you want to start it running again, you can continue the job in the foreground by using fg %1
; read your shell's manual on job control for more information.) (如果你想让它再次运行,你可以使用
fg %1
在前台继续工作;阅读你的 shell 的工作控制手册以获取更多信息。)
Alternatively, in a Unix or Unix-like environment, you can find the Python process's PID (process identifier) and kill it by PID.或者,在 Unix 或类 Unix 环境中,您可以找到 Python 进程的 PID(进程标识符)并通过 PID 将其杀死。 Use something like
ps aux | grep python
使用类似
ps aux | grep python
东西ps aux | grep python
to find which Python processes are running, and then use kill <pid>
to send a SIGTERM
signal. ps aux | grep python
查找正在运行的 Python 进程,然后使用kill <pid>
发送SIGTERM
信号。
The kill
command on Unix sends SIGTERM
by default, and a Python program can install a signal handler for SIGTERM
using the signal
module. Unix 上的
kill
命令默认发送SIGTERM
,Python 程序可以使用signal
模块为SIGTERM
安装信号处理程序。 In theory, any signal handler for SIGTERM
should shut down the process gracefully.理论上,
SIGTERM
任何信号处理程序都应该优雅地关闭进程。 But sometimes if the process is stuck (for example, blocked in an uninterruptable IO sleep state), a SIGTERM
signal has no effect because the process can't even wake up to handle it.但有时如果进程卡住(例如,在不可中断的 IO 睡眠状态中被阻塞),
SIGTERM
信号不起作用,因为该进程甚至无法唤醒来处理它。
To forcibly kill a process that isn't responding to signals, you need to send the SIGKILL
signal, sometimes referred to as kill -9
because 9
is the numeric value of the SIGKILL
constant.要强行杀死不响应信号的进程,您需要发送
SIGKILL
信号,有时称为kill -9
因为9
是SIGKILL
常量的数值。 From the command line, you can use kill -KILL <pid>
(or kill -9 <pid>
for short) to send a SIGKILL
and stop the process running immediately.从命令行,您可以使用
kill -KILL <pid>
(或简称kill -9 <pid>
)发送SIGKILL
并立即停止进程运行。
On Windows, you don't have the Unix system of process signals, but you can forcibly terminate a running process by using the TerminateProcess
function.在 Windows 上,您没有进程信号的 Unix 系统,但您可以使用
TerminateProcess
函数强制终止正在运行的进程。 Interactively, the easiest way to do this is to open Task Manager, find the python.exe
process that corresponds to your program, and click the "End Process" button.交互式地,最简单的方法是打开任务管理器,找到与您的程序对应的
python.exe
进程,然后单击“结束进程”按钮。 You can also use the taskkill
command for similar purposes.您也可以将
taskkill
命令用于类似目的。
exit()
, you can do it.exit()
的脚本中,您可以做到。pkill -f name-of-the-python-script
.pkill -f name-of-the-python-script
。To stop a running program, use Ctrl + C to terminate the process.要停止正在运行的程序,请使用Ctrl + C终止该进程。
To handle it programmatically in python, import the sys
module and use sys.exit()
where you want to terminate the program.要在 python 中以编程方式处理它,请导入
sys
模块并在要终止程序的地方使用sys.exit()
。
import sys
sys.exit()
To stop a python script using the keyboard: Ctrl + C使用键盘停止 python 脚本: Ctrl + C
To stop it using code (This has worked for me on Python 3) :使用代码停止它(这在 Python 3 上对我有用):
import os
os._exit(0)
you can also use:您还可以使用:
import sys
sys.exit()
or:或者:
exit()
or:或者:
raise SystemExit
Ctrl - Break它比Ctrl - C更强大
When I have a python script running on a linux terminal, CTRL + \\
works.当我在 linux 终端上运行 python 脚本时, CTRL +
\\
起作用。 (not CRTL + C or D ) (不是CRTL + C或D )
Ctrl + Z should do it, if you're caught in the python shell. Ctrl + Z应该这样做,如果您被python shell 捕获。 Keep in mind that instances of the script could continue running in background, so under linux you have to kill the corresponding process.
请记住,脚本的实例可以继续在后台运行,因此在 linux 下您必须杀死相应的进程。
exit() will kill the Kernel if you're in Jupyter Notebook so it's not a good idea.如果您在 Jupyter Notebook 中,exit() 将杀死内核,因此这不是一个好主意。
raise
command will stop the program. raise
命令将停止程序。
To stop your program, just press CTRL + D要停止您的程序,只需按CTRL + D
or exit()
.或
exit()
。
Control + D在 Windows 10 上对我有用。此外,将exit()
放在最后也有效。
您还可以使用Activity Monitor
来停止 py 进程
If you are working with Spyder, use CTRL + .如果您正在使用 Spyder,请使用CTRL + 。 and you will restart the kernel, also you will stop the program.
您将重新启动内核,同时您将停止该程序。
Windows solution: Control + C . Windows 解决方案: Control + C 。
Macbook solution: Control (^) + C . Macbook 解决方案: Control (^) + C 。
Another way is to open a terminal, type top
, write down the PID
of the process that you would like to kill and then type on the terminal: kill -9 <pid>
另一种方法是打开一个终端,输入
top
,记下您想杀死的进程的PID
,然后在终端上输入: kill -9 <pid>
Press Ctrl + Alt + Delete and Task Manager will pop up.按Ctrl + Alt + Delete会弹出任务管理器。 Find the Python command running, right click on it and and click Stop or Kill.
找到正在运行的 Python 命令,右键单击它,然后单击停止或终止。
拔出设备的插头,然后将其从窗口中取出
Try using:尝试使用:
Ctrl + Fn + S
Ctrl + Fn + S
or或者
Ctrl + Fn + B
Ctrl + Fn + B
If you are writing a script to process 349 files, but want to test with fewer, just write a nonexisting word like 'stop' in your list, which will cause a stop in the form of an exception.如果您正在编写一个脚本来处理 349 个文件,但想用更少的文件进行测试,只需在您的列表中写一个不存在的词,如“停止”,这将导致异常形式的停止。 This avoids dialogs like do you want to kill your process if you use
exit()
or quit()
这避免了对话框,例如如果您使用
exit()
或quit()
是否要终止您的进程
Programmatically, you could use this: 以编程方式,您可以使用以下代码:
while True:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.