长时间运行的子进程退出后清理临时文件夹

发布于 2024-09-01 13:00:52 字数 802 浏览 0 评论 0原文

我有一个 Python 脚本(在另一个应用程序内运行),它生成一堆临时图像。然后,我使用 subprocess 启动应用程序来查看这些内容。

当图像查看过程存在时,我想删除临时图像。

我无法从 Python 执行此操作,因为 Python 进程可能在子进程完成之前已退出。即我无法执行以下操作:

p = subprocess.Popen(["imgviewer", "/example/image1.jpg", "/example/image1.jpg"])
p.communicate()
os.unlink("/example/image1.jpg")
os.unlink("/example/image2.jpg")

..因为这会阻塞主线程,我也无法检查线程中存在的pid

我能想到的唯一解决方案意味着我必须使用shell=True,我宁愿避免:

import pipes
import subprocess

cmd = ['imgviewer']
cmd.append("/example/image2.jpg")

for x in cleanup:
    cmd.extend(["&&", "rm", pipes.quote(x)])

cmdstr = " ".join(cmd)
subprocess.Popen(cmdstr, shell = True)

这有效,但很难优雅。

基本上,我有一个后台子进程,并且想在它退出时删除临时文件,即使Python进程不再存在。

I have a Python script (running inside another application) which generates a bunch of temporary images. I then use subprocess to launch an application to view these.

When the image-viewing process exists, I want to remove the temporary images.

I can't do this from Python, as the Python process may have exited before the subprocess completes. I.e I cannot do the following:

p = subprocess.Popen(["imgviewer", "/example/image1.jpg", "/example/image1.jpg"])
p.communicate()
os.unlink("/example/image1.jpg")
os.unlink("/example/image2.jpg")

..as this blocks the main thread, nor could I check for the pid exiting in a thread etc

The only solution I can think of means I have to use shell=True, which I would rather avoid:

import pipes
import subprocess

cmd = ['imgviewer']
cmd.append("/example/image2.jpg")

for x in cleanup:
    cmd.extend(["&&", "rm", pipes.quote(x)])

cmdstr = " ".join(cmd)
subprocess.Popen(cmdstr, shell = True)

This works, but is hardly elegant..

Basically, I have a background subprocess, and want to remove the temp files when it exits, even if the Python process no longer exists.

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

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

发布评论

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

评论(1

帅的被狗咬 2024-09-08 13:00:52

如果您使用的是 Unix 的任何变体,您可以分叉您的 Python 程序,并让父进程继续其生命,同时子进程守护进程运行查看器(如果这会阻止子进程,那根本不重要) ,无论如何,它在生活中没有其他工作;-),并在完成后进行清理。 原始 Python进程此时可能存在,也可能不存在,但是“等待清理”子进程当然会存在(某些进程或其他进程必须执行清理工作) -毕竟,对吧?-)。

如果您使用的是 Windows,或者需要跨平台代码,那么让您的 Python 程序“生成”(即,从子进程开始,然后继续生活)另一个(小得多的)程序,这是负责运行的程序观众(阻塞,谁在乎),然后进行清理。 (如果在 Unix 上,即使在这种情况下,您也可能需要守护进程,否则子进程可能会在父进程消失时消失)。

If you're on any variant of Unix, you could fork your Python program, and have the parent process go on with its life while the child process daemonized, runs the viewer (doesn't matter in the least if that blocks the child process, which has no other job in life anyway;-), and cleans up after it. The original Python process may or may not exist at this point, but the "waiting to clean up" child process of course will (some process or other has to do the clean-up, after all, right?-).

If you're on Windows, or need cross-platform code, then have your Python program "spawn" (i.e., just start with subprocess, then go on with life) another (much smaller) one, which is the one tasked to run the viewer (blocking, who cares) and then do the clean-up. (If on Unix, even in this case you may want to daemonize, otherwise the child process might go away when the parent process does).

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