简体   繁体   English

使用Kill进程进行Python测试

[英]Python Testing with Kill Process

When testing a Python script I will often use a 'raw_input()' or 'input()' as a marker in the script, and when that marker is reached do a ctrl + c to kill the process in my command prompt. 在测试Python脚本时,我通常会在脚本中使用“ raw_input()”或“ input()”作为标记,当达到该标记时,请执行ctrl + c杀死命令提示符中的进程。

I was worried this would lead to memory leaks, this brief thread indicates it shouldn't - killing python process leads memory leak? 我担心这会导致内存泄漏,这个简短的线程表明它不应该- 杀死python进程导致内存泄漏?

It is bad practice to test scripts this way? 这样测试脚本是不好的做法? Can it lead to any negative effects? 会导致任何负面影响吗?

Usually there's no problem with using raw_input() or input() with ctrl + c to terminate your program. 通常,将raw_input()input()ctrl + c可以终止程序。 When you press ctrl + c during raw_input or input invocation you're just raising a KeyboardInterrupt exception and Python knows how to handle exceptions appropriately. raw_inputinput调用期间按ctrl + c时,您仅引发了KeyboardInterrupt异常,Python知道如何适当地处理异常。 If you don't handle the KeyboardInterrupt , this exception will be handled by the default top-level exception handler, this default exception handler prints the stack trace from which the exception occurred just before exiting the interpreter. 如果不处理KeyboardInterrupt ,则此异常将由默认的顶级异常处理程序处理,此默认异常处理程序将在退出解释器之前打印发生异常的堆栈跟踪。

For some applications, they might not free their resources after they exit or after you kill the process, most operating systems today are smart enough to free memory when its unused. 对于某些应用程序,在退出或终止进程后,它们可能无法释放资源,当今的大多数操作系统足够聪明,可以在不使用内存时释放内存。 But this is not applicable to your question. 但这不适用于您的问题。

Not sure about the leaks but I'm sure breakpoints are your friend here. 不确定泄漏,但是断点是您的朋友。 In idle right click on the line you want to stop on and click set breakpoint, then clear when you're finished debugging. 在空闲状态下,右键单击要停止的行,然后单击设置断点,然后在完成调试后清除。

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

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