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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19782075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:37:20  来源:igfitidea点击:

How to stop/terminate a python script from running?

pythonexecutionterminatetermination

提问by MACEE

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?

我在 IDLE 中编写了一个程序来标记文本文件,它开始标记 349 个文本文件!我怎样才能阻止它?如何停止正在运行的 Python 程序?

采纳答案by Christian

To stop your program, just press Control+ C.

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

回答by Dennis

you can also use the Activity Monitorto stop the py process

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

回答by Amir

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

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

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

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

回答by wpp

  • 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.
  • 要停止 python 脚本,只需按Ctrl + C
  • 在带有 的脚本中exit(),您可以做到。
  • 您只需退出即可在交互式脚本中执行此操作。
  • 您可以使用pkill -f name-of-the-python-script.

回答by Scott P.

Ctrl-Breakit is more powerful than Ctrl-C

Ctrl-Break它比Ctrl-C更强大

回答by Vivek Parmar

To stop your program, just press CTRL+ D

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

or exit().

exit()

回答by Juke

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.

Ctrl + Z 应该这样做,如果您被python shell 捕获。请记住,脚本的实例可以继续在后台运行,因此在 linux 下您必须杀死相应的进程。

回答by Chandella07

To stop a running program, use CTRL+Cto terminate the process.

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

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

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

import sys
sys.exit()

回答by Andrew Hyder

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

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

回答by Stefun06

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

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