64-bit ArcPy Not releasing Python Process on Completion

4884
18
Jump to solution
06-11-2020 07:02 AM
EricFulcher1
New Contributor III

I'm using a command script to run an arcpy process on ArcMap 10.6 - 64-bit arcpy. On successfully completing the script, the process ends - but doesn't release/kill the python.exe (or pythonw.exe), therefore the bat script hangs and doesn't finish. In the script below, its the arcpy.mapping.ListDataFrames that causes the process to hang (that was fun narrowing down...). Comment it out and python terminates correctly. If i run this in 32 bit python terminates correctly - so just 64 bit. I'm thinking esri bug? Am I missing something?

Thanks,
Eric

Cleaned up script (may have missed something here) but it says the script is Finished.

import arcpy
import string
import gc


def main():
print("Completion Script is Starting")

mxd = arcpy.mapping.MapDocument("D:\WOBufferScripts\LeakSurveyScripts.mxd")
# Running this causes python to not close.
dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]
del dataFrame
del mxd
gc.collect()
print("Completion Script Has Finished")

if __name__ == "__main__":
main()

Tags (1)
0 Kudos
18 Replies
JoshuaBixby
MVP Esteemed Contributor

I assume this is still in relation to Oracle EGDB FCs, so this reply is coming from that angle/perspective.

after we upgraded to ArcServer 10.8

Can you elaborate?  Did you mean you upgraded the Oracle EGDB schema to 10.8?  Or, are you running the scripts from ArcPy on ArcGIS Enterprise machines and those were upgraded to 10.8?  Or, do you mean both?

0 Kudos
ChrisWalsh
New Contributor

Apologies, the update to 10.8 seems to have nothing to do with it. I tried running our automated publishing script against a 10.6 environment and the same thing is happening. 10.6.1 Server, 10.6.1 schema.

0 Kudos
PaulBatley
New Contributor III

Hey, We're getting the same issue for both 32 & 64bit and it only started happening around an installed Windows update KB4593226. Prior to the update our python jobs running via Scheduled Tasks runs through but will not run on the next schedule as the process is already running, due to the fact the process never terminates. We have debugged sys.exit() and it is firing correctly. Our only workaround is to have the following code:
os.kill(os.getpid(),9)
This ensures the process terminates and that batch processing continues.

RTRC_BOGLicense_Mgr
New Contributor

Hi Paul, I have the same problem, only with your suggestion the process ends. Thanks.

PaulBatley
New Contributor III

Hi,
Just to update further. After KB4598243 which superceded the patch mentioned above, the issue started affecting the Interop tool too. With the FME.exe process now not closing down and causing a further problem as the file handles for the logging were inherited and therefore the python Scheduled Task couldn't re-run because of the orphaned FME process. The code below resolved the orphaned FME not shutting down with the Standard signum (,9)  kill signal.

## Python for dealing with child processes
parentProcess = psutil.Process(os.getpid())
children = parentProcess.children(recursive=True)
for ofspring in children:
    localLogger.write("Child ProcessID: {}".format(ofspring.pid) + " terminating...")
    os.kill(ofspring.pid,9)
localLogger.write("Python Process PID: {}".format(os.getpid()) + " terminating...")
os.kill(os.getpid(),9)

EricFulcher1
New Contributor III

Sweet - I'm going to so try this.

Thanks!

willemjenniskens
New Contributor

Great solution! This fixes it for me and should be marked as the solution imo. 

EricFulcher1
New Contributor III

If I could figure out how to change the solution flag - I would. Somebody looking for points marked it as solved.... But yes this is the solution.

DarrylKlassen1
Occasional Contributor II

Thanks, this worked great for me.  My PY was completing but not exiting and moving on to the next script in the BAT file.  Once I added that piece of code at the end, works great.