在使用 Python 的 Windows 中,如何终止我的进程?

发布于 2024-10-19 10:17:28 字数 557 浏览 3 评论 0原文

编辑:看起来像重复的,但我向你保证,它不是。我希望干净地终止当前正在运行的进程,而不是终止单独的进程。

问题是我要杀死的进程不是由 subprocess 或 exec 产生的。它基本上是想自杀。

场景如下:程序在退出时进行清理,但有时这需要很长时间。我确信我可以终止该程序,因为退出的第一步会保存数据库。我该怎么做呢?

  • 无法使用taskkill,因为它在某些Windows安装中不可用,例如家庭版XP
  • tskill也不起作用

  • win32api.TerminateProcess(handle, 0) 有效,但我担心它可能会导致内存泄漏,因为我没有机会关闭句柄(程序在调用 TerminateProcess 后立即停止)。注意:是的,我强制退出它,所以肯定会有一些未释放的资源,但我想尽可能地减少这种情况(因为只有在花费了难以忍受的时间时我才会这样做,为了更好的用户经验)但我不认为Python会处理gc,如果它是强制退出。

我目前正在做最后一项,因为它确实有效。但我担心未释放的句柄。任何想法/建议将非常感激!

Edit: Looks like a duplicate, but I assure you, it's not. I'm looking to kill the current running process cleanly, not to kill a separate process.

The problem is the process I'm killing isn't spawned by subprocess or exec. It's basically trying to kill itself.

Here's the scenario: The program does cleanup on exit, but sometimes this takes too long. I am sure that I can terminate the program, because the first step in the quit saves the Database. How do I go about doing this?

  • cannot use taskkill, as it is not available in some Windows installs e.g. home editions of XP
  • tskill also doesn't work

  • win32api.TerminateProcess(handle, 0) works, but i'm concerned it may cause memory leaks because i won't have the opportunity to close the handle (program immediately stops after calling TerminateProcess). note: Yup, I am force quitting it so there are bound to be some unfreed resources, but I want to minimize this as much as possible (as I will only do it only if it is taking an unbearable amount of time, for better user experience) but i don't think python will handle gc if it's force-quit.

I'm currently doing the last one, as it just works. I'm concerned though about the unfreed handle. Any thoughts/suggestions would be very much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀手六號 2024-10-26 10:17:28

win32api.TerminateProcess(句柄, 0)
有效,但我担心它可能会导致
内存泄漏,因为我没有
有机会关闭手柄
(程序立即停止后
调用 TerminateProcess)。注意:是的,
我被迫退出所以有
必然是一些未释放的资源,
但我想尽量减少这种情况
可能(因为只有在以下情况下我才会这样做
它需要大量的
时间,以获得更好的用户体验)但是
我不认为 python 会处理 gc 如果
这是强制退出。

如果进程自行终止,那么您无需担心垃圾收集。操作系统会自动清理该进程使用的所有内存资源,因此您不必担心内存泄漏。内存泄漏是指进程运行并随着时间的推移使用越来越多的内存。

所以,是的,以这种方式终止进程并不是很“干净”,但不会有任何不良副作用。

win32api.TerminateProcess(handle, 0)
works, but i'm concerned it may cause
memory leaks because i won't have the
opportunity to close the handle
(program immediately stops after
calling TerminateProcess). note: Yup,
I am force quitting it so there are
bound to be some unfreed resources,
but I want to minimize this as much as
possible (as I will only do it only if
it is taking an unbearable amount of
time, for better user experience) but
i don't think python will handle gc if
it's force-quit.

If a process self-terminates, then you don't need to worry about garbage collection. The OS will automatically clean up all memory resources used by that process, so you don't have to worry about memory leaks. Memory leaks are when a process is running and using more and more memory as time goes by.

So yes terminating your process this way isn't very "clean", but there wont be any ill side-effects.

瑶笙 2024-10-26 10:17:28

如果我理解你的问题,那么你正试图让程序自行关闭。这通常是通过 sys.exit() 完成的。

If I understand your question, you're trying to get the program to shut itself down. This is usually done with sys.exit().

追风人 2024-10-26 10:17:28

TerminateProcess 和 taskkill /f 不会释放资源,会导致内存泄漏。
这是 MS 对终止进程的引用:
{ ...终止进程不会导致子进程终止。
终止进程并不一定会从系统中删除进程对象。当进程的最后一个句柄关闭时,进程对象将被删除。 ... }
MS 大量使用 COM 和 DCOM,它们共享操作系统没有也无法跟踪的句柄和资源。如果您不打算经常重新启动,则应使用 ExitProcess。这允许进程正确释放其使用的资源。 Linux不存在这个问题,因为它不使用COM或DCOM。

TerminateProcess and taskkill /f do not free resources and will result in memory leaks.
Here is the MS quote on terminateProcess:
{ ... Terminating a process does not cause child processes to be terminated.
Terminating a process does not necessarily remove the process object from the system. A process object is deleted when the last handle to the process is closed. ... }
MS heavily uses COM and DCOM, which share handles and resources the OS does not and can not track. ExitProcess should then be used instead, if you do not intend to reboot often. That allows a process to properly free the resources it used. Linux does not have this problem because it does not use COM or DCOM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文