在 mac 上的终端中停止 python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18047657/
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 09:48:00  来源:igfitidea点击:

stop python in terminal on mac

pythonmacosterminal

提问by lukmac

Using python in terminal on a Mac, type

在 Mac 上的终端中使用 python,键入

ctrl-z

will stop the python, but not exit it, giving output like this:

将停止 python,但不退出它,给出如下输出:

>>> 
[34]+  Stopped                 python

As you can see, I have stopped 34 python calls.

如您所见,我已经停止了 34 个 python 调用。

Although I could use

虽然我可以用

>>> exit()

to exit python, the questions are:

退出python,问题是:

  1. Is there a short-key to really exit (not just stop) python in terminal? and, why

    ctrl-c

    does NOT work?

  2. How do I kill all the stopped python?

  1. 是否有一个快捷键可以在终端中真正退出(不仅仅是停止)python?而且,为什么

    ctrl-c

    不工作?

  2. 我如何杀死所有停止的蟒蛇?

BTW, how could I type 'ctrl-c' and other hotkeys with a keyboard look?

顺便说一句,我怎么能像键盘一样输入“ctrl-c”和其他热键?

Thanks!

谢谢!

回答by user2522001

CTRL+d-> Defines EOF (End of File).

CTRL+ d-> 定义 EOF(文件结尾)。

CTRL+c-> Will terminate most jobs.

CTRL+ c-> 将终止大多数工作。

If, however you have written a python wrapper program that calls other python programs in turn, Ctrl-c will only stop the the job that is currently running. The wrapper program will keep running. Worst case scenario, you can do this:

但是,如果您编写了一个 Python 包装程序来依次调用其他 Python 程序,则 Ctrl-c 只会停止当前正在运行的作业。包装程序将继续运行。最坏的情况,你可以这样做:

Open up: Applications -> Utilities -> Activity Monitor, Find the process labeled as python, highlight it in the activity monitor then click on "Quit Process".

打开:应用程序 -> 实用程序 -> 活动监视器,找到标记为 python 的进程,在活动监视器中突出显示它,然后单击“退出进程”。

These three suggestions should work for most situations where you want a program to stop.

这三个建议应该适用于您希望程序停止的大多数情况。

回答by Ned Deily

This isn't a Python issue, per se. This ia a Unix shell behavior and is a difference from Windows and other systems. See the Wikipedia article on CTRL-Zfor a more complete explanation:

这本身不是 Python 问题。这是一种 Unix shell 行为,与 Windows 和其他系统不同。有关CTRL-Z的更完整解释,请参阅维基百科文章

On Unix-like systems, Control+Z is the most common default keyboard mapping for the key sequence that suspends a process (SIGTSTP). When entered by a user at their computer terminal, the currently running foreground process is sent a SIGTSTP signal, which generally causes the process to suspend its execution. The user can later continue the process execution by typing the command 'fg' (short for foreground) or by typing 'bg' (short for background) and furthermore typing the command 'disown' to separate the background process from the terminal.

在类 Unix 系统上,Control+Z 是挂起进程的键序列 (SIGTSTP) 的最常见的默认键盘映射。当用户在他们的计算机终端输入时,当前运行的前台进程会被发送一个 SIGTSTP 信号,这通常会导致进程暂停其执行。用户稍后可以通过键入命令“fg”(前台的缩写)或键入“bg”(后台的缩写)并进一步键入命令“disown”以将后台进程与终端分开来继续进程执行。

On OS X as on various other Unix-based systems, you could use the sttycommand to change which key, if any, produces a SIGTSTP(or an eof, for that matter):

在 OS X 和其他各种基于 Unix 的系统上,您可以使用该stty命令来更改哪个键(如果有)产生 a SIGTSTP(或 an eof,就此而言):

$ stty -a
speed 38400 baud; 30 rows; 90 columns;
lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl
    -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
    -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
    -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
    -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
    eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
    min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
    stop = ^S; susp = ^Z; time = 0; werase = ^W;
$ stty susp ^Y
$ cat
^Y
[1]+  Stopped                 cat