问题:如何停止/终止python脚本的运行?

我用IDLE编写了一个程序来标记文本文件,然后开始标记349个文本文件!我该如何阻止它?如何停止正在运行的Python程序?

I wrote a program in IDLE to tokenize text files and it starts to tokeniza 349 text files! How can I stop it? How can I stop a running Python program?


回答 0

要停止程序,只需按Control + C

To stop your program, just press Control + C.


回答 1

如果exit()在代码中使用该函数,也可以执行此操作。更理想的是,您可以这样做sys.exit()。即使您通过软件包并行运行程序sys.exit()也可能终止Python multiprocessing

注意:为了使用sys.exit(),您必须将其导入:import sys

You can also do it if you use the exit() function in your code. More ideally, you can do sys.exit(). sys.exit() which might terminate Python even if you are running things in parallel through the multiprocessing package.

Note: In order to use the sys.exit(), you must import it: import sys


回答 2

如果您的程序在交互式控制台上运行,则按CTRL+CKeyboardInterrupt在主线程上引发异常。

如果您的Python程序没有捕获到它,KeyboardInterrupt则会导致Python退出。但是,一个except KeyboardInterrupt:块或类似裸露的东西except:将阻止此机制实际停止脚本的运行。

有时,如果KeyboardInterrupt无法正常工作,您可以发送SIGBREAK信号。在Windows上,解释器可以处理CTRL+Pause/Break而不生成可捕获的KeyboardInterrupt异常。

但是,这些机制主要仅在Python解释器正在运行并响应操作系统事件时才起作用。如果Python解释器由于某种原因没有响应,则最有效的方法是终止运行解释器的整个操作系统进程。其机制因操作系统而异。

在Unix风格的shell环境,您可以按CTRL+Z中止任何进程当前控制的控制台。返回shell提示后,您可以使用jobs它列出挂起的作业,并可以使用杀死第一个挂起的作业kill %1。(如果要重新开始运行,可以使用fg %1;在前台继续执行作业;有关更多信息,请阅读Shell的作业控制手册。)

另外,在Unix或类似Unix的环境中,您可以找到Python进程的PID(进程标识符)并用PID杀死它。使用类似的方法ps aux | grep python来查找正在运行的Python进程,然后使用kill <pid>发送SIGTERM信号。

killUnix上的命令SIGTERM默认发送,Python程序可以安装信号处理程序以SIGTERM使用该signal模块。从理论上讲,任何用于的信号处理程序SIGTERM都应正常关闭进程。但是有时,如果进程卡住了(例如,在不间断的IO睡眠状态下被阻塞),则SIGTERM信号无效,因为该进程甚至无法唤醒以处理该信号。

为了强行终止不响应信号的进程,您需要发送SIGKILL信号,有时称为,kill -9因为它9SIGKILL常量的数值。在命令行中,您可以使用kill -KILL <pid>(或kill -9 <pid>简称)发送aSIGKILL并立即停止该进程运行。

在Windows上,您没有Unix的进程信号系统,但是您可以使用TerminateProcess函数强制终止正在运行的进程。以交互方式,最简单的方法是打开任务管理器,找到python.exe与您的程序相对应的进程,然后单击“结束进程”按钮。您也可以将taskkill命令用于类似目的。

If your program is running at an interactive console, pressing CTRL + C will raise a KeyboardInterrupt exception on the main thread.

If your Python program doesn’t catch it, the KeyboardInterrupt will cause Python to exit. However, an except KeyboardInterrupt: block, or something like a bare except:, will prevent this mechanism from actually stopping the script from running.

Sometimes if KeyboardInterrupt is not working you can send a SIGBREAK signal instead; on Windows, CTRL + Pause/Break may be handled by the interpreter without generating a catchable KeyboardInterrupt exception.

However, these mechanisms mainly only work if the Python interpreter is running and responding to operating system events. 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. 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. 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. (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.)

Alternatively, in a Unix or Unix-like environment, you can find the Python process’s PID (process identifier) and kill it by PID. Use something like ps aux | grep python to find which Python processes are running, and then use kill <pid> to send a SIGTERM signal.

The kill command on Unix sends SIGTERM by default, and a Python program can install a signal handler for SIGTERM using the signal module. In theory, any signal handler for SIGTERM should shut down the process gracefully. 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.

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. 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.

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. 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. You can also use the taskkill command for similar purposes.


回答 3

  • 要停止python脚本,只需按Ctrl + C
  • 在脚本中exit(),您可以执行此操作。
  • 您可以在退出后的交互式脚本中进行操作。
  • 您可以使用pkill -f name-of-the-python-script
  • To stop a python script just press Ctrl + C.
  • Inside a script with exit(), you can do it.
  • You can do it in an interactive script with just exit.
  • You can use pkill -f name-of-the-python-script.

回答 4

Ctrl-BreakCtrl-C更强大

Ctrl-Break it is more powerful than Ctrl-C


回答 5

要停止正在运行的程序,请使用Ctrl+C终止该过程。

要在python中以编程方式处理它,请导入sys模块并sys.exit()在要终止程序的位置使用。

import sys
sys.exit()

To stop a running program, use Ctrl+C to terminate the process.

To handle it programmatically in python, import the sys module and use sys.exit() where you want to terminate the program.

import sys
sys.exit()

回答 6

要使用键盘停止python脚本,请执行以下操作:Ctrl+C

使用代码停止它(这在Python 3上对我有用):

import os
os._exit(0)

您还可以使用:

import sys
sys.exit()

要么:

exit()

要么:

raise SystemExit

To stop a python script using the keyboard: Ctrl + C

To stop it using code (This has worked for me on Python 3) :

import os
os._exit(0)

you can also use:

import sys
sys.exit()

or:

exit()

or:

raise SystemExit

回答 7

如果您陷入python shell中,请按Ctrl +Z。请记住,脚本实例可以在后台继续运行,因此在Linux下,您必须终止相应的进程。

Ctrl + Z should do it, if you’re caught in the 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.


回答 8

当我在Linux终端上运行python脚本时,CTRL + \起作用。(不是CRTL + C或D)

When I have a python script running on a linux terminal, CTRL + \ works. (not CRTL + C or D)


回答 9

Control+D在Windows 10上对我有效。此外,放在exit()末尾也适用。

Control+D works for me on Windows 10. Also, putting exit() at the end also works.


回答 10

如果您在Jupyter Notebook中,则exit()会杀死内核,所以这不是一个好主意。raise命令将停止程序。

exit() will kill the Kernel if you’re in Jupyter Notebook so it’s not a good idea. raise command will stop the program.


回答 11

您还可以使用Activity Monitor停止py进程

you can also use the Activity Monitor to stop the py process


回答 12

要停止程序,只需按CTRL+D

exit()

To stop your program, just press CTRL + D

or exit().


回答 13

Ctrl+ Alt+ Delete,将弹出任务管理器。找到正在运行的Python命令,右键单击它,然后单击“停止”或“杀死”。

Press Ctrl+Alt+Delete and Task Manager will pop up. Find the Python command running, right click on it and and click Stop or Kill.


回答 14

如果您正在使用Spyder,请使用CTRL +。 (DOT),您将重新启动内核,也将停止程序。

If you are working with Spyder, use CTRL + . (DOT) and you will restart the kernel, also you will stop the program.


回答 15

Windows解决方案:Control + C

Macbook解决方案:Control (^) + C

另一种方法是打开一个终端,键入top,记下PID您想杀死的进程,然后在终端上键入:kill -9 <pid>

Windows solution: Control + C.

Macbook solution: 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>


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。