{"id":251,"date":"2013-05-14T10:31:21","date_gmt":"2013-05-14T14:31:21","guid":{"rendered":"http:\/\/www.tks-designs.com\/blog\/?p=251"},"modified":"2013-05-14T10:31:21","modified_gmt":"2013-05-14T14:31:21","slug":"terminating-a-child-process-from-python","status":"publish","type":"post","link":"https:\/\/www.tks-designs.com\/blog\/?p=251","title":{"rendered":"Terminating a child process from python"},"content":{"rendered":"<p>I&#8217;ve been working on improving my renderfarm, and I&#8217;ve run into some trouble trying to close clients remotely. You can easily end a python process with <code>sys.exit()<\/code>, however if a rendering application (say Nuke) has been spawned by the script, it will not close. After some digging (and a bunch of great help from <a href=\"http:\/\/stackoverflow.com\/questions\/16401031\/python-multiprocessing-pool-terminate\/\" target=\"_blank\">stackOverflow<\/a>) I seem to have come up with a solution. When you call a 3rd party application with either call() or:<\/p>\n<pre><code>process = Popen(allRenderArg, env=os.environ)<\/code><\/pre>\n<p>Python will create a tiny, dummy process which only exists to call that application. When you exit your script, python will clean up those dummy processes, but won&#8217;t kill child processes, thereby leaving the renders going.<\/p>\n<p>The solution that I&#8217;ve come up with is to get the pid of those dummy python processes, then use some unix commands to find the pids of their children, and kill them with <code>os.kill()<\/code>. If this was a linux platform I could do it in a more efficient way (possibly using <code>pstree<\/code>) but on OSX I have to use grep and some fancy code:<\/p>\n<pre><code>processId = process.pid\r\nprint \"attempting to terminate \"+str(processId)\r\ncommand = \" ps -o pid,ppid -ax | grep \"+str(processId)+\" | cut -f 1 -d \\\" \\\" | tail -1\"\r\nps_command = Popen(command, shell=True, stdout=PIPE)\r\nps_output = ps_command.stdout.read()\r\nretcode = ps_command.wait()\r\nassert retcode == 0, \"ps command returned %d\" % retcode\r\nprint \"child process pid: \"+ str(ps_output)\r\nos.kill(int(ps_output), signal.SIGTERM)\r\nos.kill(int(processId), signal.SIGTERM)<\/code><\/pre>\n<\/p>\n<p>There might be a nicer way, but I don&#8217;t know it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on improving my renderfarm, and I&#8217;ve run into some trouble trying to close clients remotely. You can easily end a python process with sys.exit(), however if a rendering application (say Nuke) has been spawned by the script, it will not close. After some digging (and a bunch of great help from stackOverflow) <span class=\"ellipsis\">&hellip;<\/span> <span class=\"more-link-wrap\"><a href=\"https:\/\/www.tks-designs.com\/blog\/?p=251\" class=\"more-link\"><span>Continue Reading &rarr;<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"kia_subtitle":"fixing a problem with calling 3rd party applications as subprocesses","footnotes":""},"categories":[16,4,17,12,7],"tags":[],"class_list":["post-251","post","type-post","status-publish","format-standard","hentry","category-osx","category-python","category-renderfarming","category-technical-direction","category-unix"],"_links":{"self":[{"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=251"}],"version-history":[{"count":5,"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":256,"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/251\/revisions\/256"}],"wp:attachment":[{"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tks-designs.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}