Hi all,
I’d like something like this.
subprocess.PopenIdeally, it dies no matter how the parent died; be it normally or when crashing. And ideally, it’d be cross-platform, and not rely on third-party module(s), though that’s flexible.
Here’s what I’ve got at the moment.
import atexit
import subprocess
proc = subprocess.Popen(["python"], creationflags=subprocess.CREATE_NEW_CONSOLE)
def kill_child():
proc.kill()
atexit.register(kill_child)
This works standalone, and in Nuke, but not in Maya. It also doesn’t run when a process is force-quitted/crashed.
So I went a little deeper, looking into OS-specific methods of doing it and found this for Windows.
http://stackoverflow.com/a/23587108/478949
Which works, but relies on a library 25mb in size and quite elaborate bundling requirements.
So then I came across this, which provides an example (at the end) that I can’t quite wrap my head around, but seems to do what I want.
http://stefan.sofa-rockers.org/2013/08/15/handling-sub-process-hierarchies-python-linux-os-x/
But, I can’t successfully adapt it; mainly because I simply don’t understand it well enough. So I was hoping to get some pointers here, either about how to adapt it to my scenario or suggest alternatives.
Any ideas?
Best,
Marcus
I might add that, from what I can tell, the Windows method of achieving this is apparently quite different and more difficult than the Unix version, so although Unix suggestions would be great, I'm mainly looking for a solution which works well on Windows; even if it's Windows-only.Thanks
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODST502Tu2093d2fJ4zxMm1eZaC2BkjYxTqjv21MhmXjQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Ok yea, then that is what my colleague thought you meant. His solution looks like this, but it is linux-only:
import signal
import subprocess
from ctypes import cdll
libc = cdll.LoadLibrary('libc.so.6')
child = subprocess.Popen(["/bin/sleep", "100"], preexec_fn=lambda *args: libc.prctl(1, signal.SIGTERM, 0, 0, 0))
That would use the C prctrl to set the child death signal to SIGTERM when the parent dies.
So yea, it isn’t portable, and you would have to search for whatever the equivalent is in Windows or BSD. That is probably why you might have to just take on a cross-platform library that handles this stuff.
On Thu, Mar 5, 2015 at 9:23 PM Marcus Ottosson <konstr...@gmail.com> wrote:
Good point, it is the other way; A launches B which launches C. That is, Maya launches the child process.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODKexSh7PU7TJmoyYgzmyrJPdCKNYroW64-cEDRbGny1Q%40mail.gmail.com.
Yeah, that's not particularly Windows-friendly unfortunately, and I don't know of any cross-platform library that does this. Thanks for sharing anyway.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODL%3DOPW-whTiYiMiHAPsEhTJyiGm%3DHjNMc2nLR8-ea08Q%40mail.gmail.com.
That’s actually the same solution I posted in my initial post, and it’s based on a non-standard library (pywin32) which is a little tricky to bundle and is quite large, but yes, based on only a few tests, it seems to do the job.
It seems to differ across Windows distributions though.
From the stackoverflow post:
One downside to using a job object is that when running on Vista or Win7, if your program is launched from the Windows shell (i.e., by clicking on an icon), then there will probably already be a job object assigned and trying to create a new job object will fail. Win8 fixes this (by allowing job objects to be nested), or if your program is run from the command line then it should be fine.
Would you mind having a gander at this and see what you can make of it?
It’s the recursive nature of how the script launches itself that throws me off, but other than that the results look to be what I’m after.
That’s actually the same solution I posted in my initial post, and it’s based on a non-standard library (pywin32) which is a little tricky to bundle and is quite large, but yes, based on only a few tests, it seems to do the job.
It seems to differ across Windows distributions though.
From the stackoverflow post:
One downside to using a job object is that when running on Vista or Win7, if your program is launched from the Windows shell (i.e., by clicking on an icon), then there will probably already be a job object assigned and trying to create a new job object will fail. Win8 fixes this (by allowing job objects to be nested), or if your program is run from the command line then it should be fine.
Would you mind having a gander at this and see what you can make of it?
It’s the recursive nature of how the script launches itself that throws me off, but other than that the results look to be what I’m after.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAKPKXRd_9XwfD%2B8az-yrpd5K32DQca6Z3gqggSzQ-_mQ%40mail.gmail.com.
No problem, Justin, it’s not terribly obvious that it’s using pywin32 as it isn’t showing up in the imports; it looks like it’s got quite a few different names, all prefixed win32.
A ctypes approach seems reasonable. Haven’t considered it beyond a few non-working examples, but I bet it must be able to deal with Job objects too.
Other than that, there is one detail about my requirements which might enable another method or achieving what I want; which is that the child is just another Python interpreter under my control. So what I could do is pass the pid of the parent to the child and have it poll every n-seconds for whether the parent is still running. If not, clean-up and self-destruct.
Though polling a process for activity seems a challenge on it’s own.. (on Windows, that is. Sigh)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCEEX84wvO1X2J4Ra9BnEMdX6HZqe%3Dk83vB4p%2B6Z0cAnw%40mail.gmail.com.
On posix, the value of the parent pid changes if it dies and the child orphans , to either 1 or 0. Not sure what the behaviour is on Windows. You would have to be careful about trying to poll exactly for a specific pid of the parent since it is possible for the pid to get reused.
The actual polling should be really easy, even without psutil, just using the "os" module and a thread.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAD%3DwhWN7x857idRaX3ve2NvFiVNVG-zQSk2jSS2y9M55eyTOjw%40mail.gmail.com.
On posix, the value of the parent pid changes if it dies and the child orphans , to either 1 or 0. Not sure what the behaviour is on Windows. You would have to be careful about trying to poll exactly for a specific pid of the parent since it is possible for the pid to get reused.
The actual polling should be really easy, even without psutil, just using the "os" module and a thread.
On Sat, 7 Mar 2015 7:58 AM Fredrik Averpil <fredrik...@gmail.com> wrote:
I think you can poll a pid using psutil. Check out its Process class.
// Ffre 6 mar 2015 kl. 16:46 skrev Marcus Ottosson <konstr...@gmail.com>:
--No problem, Justin, it’s not terribly obvious that it’s using pywin32 as it isn’t showing up in the imports; it looks like it’s got quite a few different names, all prefixed
win32.A
ctypesapproach seems reasonable. Haven’t considered it beyond a few non-working examples, but I bet it must be able to deal withJobobjects too.Other than that, there is one detail about my requirements which might enable another method or achieving what I want; which is that the child is just another Python interpreter under my control. So what I could do is pass the
pidof theparentto thechildand have it poll every n-seconds for whether theparentis still running. If not, clean-up and self-destruct.Though polling a process for activity seems a challenge on it’s own.. (on Windows, that is. Sigh)
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCEEX84wvO1X2J4Ra9BnEMdX6HZqe%3Dk83vB4p%2B6Z0cAnw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Thanks a lot, guys. psutil is looking amazing so far.
I mean, how cool is this?
# Kill children of process id 1234
import psutil
for child in psutil.Process(1234).children():
child.kill()
For polling, I’m thinking something like this will suffice.
psutil.Process(1234).wait()
sys.exit()
On windows its a heavy handed operation to get a parent process id
I did find a pretty straightforward way to do this:
import os
os.getpid()
Until a better solution pops up, this fits the bill I think.
Thanks!
On Sun, 8 Mar 2015 8:22 AM Marcus Ottosson <konstr...@gmail.com> wrote:
Thanks a lot, guys. psutil is looking amazing so far.
I mean, how cool is this?
# Kill children of process id 1234 import psutil for child in psutil.Process(1234).children(): child.kill()
For polling, I’m thinking something like this will suffice.
psutil.Process(1234).wait() sys.exit()
On windows its a heavy handed operation to get a parent process id
I did find a pretty straightforward way to do this:import os os.getpid()
Not heavy to get the pid of the current process. Heavy to do the whole parent process lookup. From the win32 calls, it looked like psutil needed to cache the ppid because it involves pulling all processes and comparing the id. psutil does it all in its windows specific code
Until a better solution pops up, this fits the bill I think.
Thanks!
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCGn0MFu1zvvqJLuuAVOv9e8ZP%2BXmHWJos8s9NfYgp08g%40mail.gmail.com.