Close Image with subprocess

我怕爱的太早我们不能终老 提交于 2019-12-25 08:49:24

问题


I am trying to open an image with subprocess so it is visible to the user, then close the image so that it disapears.

This question has been asked before, but the answers I found have not worked for me. Here is what I have checked:

Killing a process created with Python's subprocess.Popen()

How to terminate a python subprocess launched with shell=True

How can I close an image shown to the user with the Python Imaging Library?

I need the code to open an image (with Preview (optional), default on my Mac), wait a second, then close the image.

openimg = subprocess.Popen(["open", mypath])
time.sleep(1)
openimg.terminate()
openimg.kill()

Everything is telling me to terminate() and kill(), but that still isn't closing the image.

This does not HAVE to use preview, I am open to hearing other options as well.

Edit: I have additionally tried the below code, still no success

print('openimg\'s pid = ',openimg.pid)
os.kill(openimg.pid, signal.SIGKILL)

回答1:


OS X's open command is asynchronous - it spawns a Preview process (or whatever app it launches) and immediately exits. So by the time your Python code gets around to calling terminate() and kill(), the open process is done. It no longer exists.

You can force synchronous behavior, i.e. make open keep running until after Preview exits, by passing the -W option,

subprocess.Popen(["open", "-W", mypath])

This way, open will still be running when your code gets around to running terminate and kill. (I would suggest also passing the -n option to make sure Preview starts a new instance, in case you had another instance of Preview sitting around from before.) And when the open process ends, hopefully it will also end the underlying Preview process. You can check whether this actually happens using a process viewer such as ps or pgrep.

If terminating or killing open does not kill Preview, you'll need to either change the configuration so that the signal is delivered to all subprocesses of open when you call terminate() or kill(), for which this question or this one may be helpful, or you'll have to find a way to get the process ID of Preview and send signals to that directly, which will require you to go beyond Popen. I'm not sure of the best way to do that, but perhaps someone else can contribute an answer that shows you how.




回答2:


I'm sure this is the hackiest way to do this, but hell, it works. If anyone stumbles on this and knows a better way, please let me know.

The Problem As David Z described, I was not targeting the correct process. So I had to figure out what the correct process ID was in order to close it properly.

I started by opening my terminal and entering the command ps -A. This will show all of the current open processes and their ID. Then I just searched for Preview and found two open processes.

Previously, I was closing the first Preview pid in the list, let's call it 11329 but the second on the list was still open. The second Preview process, 11330, was always 1 digit higher then the first process. This second one was the pid I needed to target to close Preview.

The Answer

openimg = subprocess.Popen(["open", mypath]) ##Opens the image to view
newpid = openimg.pid + 1 ##Gets the pid 1 digit higher than the openimg pid. 
os.kill(newpid, signal.SIGKILL) ##Kills the pid and closes Preview

This answer seems very fragile, but it works for me. I only just started learning about pids so if anyone could provide some knowledge, I would be grateful.



来源:https://stackoverflow.com/questions/41195639/close-image-with-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!