Python subprocess.Popen erroring with OSError: [Errno 12] Cannot allocate memory after period of time

前端 未结 9 1586
南方客
南方客 2020-12-06 01:53

Note: This question has been re-asked with a summary of all debugging attempts here.


I have a Python script that is running as a background pr

相关标签:
9条回答
  • 2020-12-06 02:33

    I don't think that the circumstances given in the Zenoss article you linked to is the only cause of this message, so it's not clear yet that swap space is definitely the problem. I would advise logging some more information even around successful calls, so that you can see the state of free memory every time just before you do the ps call.

    One more thing - if you specify shell=True in the Popen call, do you see different behaviour?

    Update: If not memory, the next possible culprit is indeed file handles. I would advise running the failing command under strace to see exactly which system calls are failing.

    0 讨论(0)
  • 2020-12-06 02:36

    when you use popen you need to hand in close_fds=True if you want it to close extra file descriptors.

    creating a new pipe, which occurs in the _get_handles function from the back trace, creates 2 file descriptors, but your current code never closes them and your eventually hitting your systems max fd limit.

    Not sure why the error you're getting indicates an out of memory condition: it should be a file descriptor error as the return value of pipe() has an error code for this problem.

    0 讨论(0)
  • 2020-12-06 02:42

    Have you watched your process over time?

    • lsof
    • ps -aux | grep -i pname
    • top

    All should give interesting information. I am thinking that the process is tying up resources that should be freed up. Is there a chance that it is tying up resource handles (memory blocks, streams, file handles, thread or process handles)? stdin, stdout, stderr from the spawned "ps". Memory handles, ... from many small incremental allocations. I would be very interested in seeing what the above commands display for your process when it has just finished launching and running for the first time and after 24 hours of "sitting" there launching the sub-process regularly.

    Since it dies after a few days, you could have it run for only a few loops, and then restart it once a day as a workaround. That would help you in the meantime.

    Jacob

    0 讨论(0)
  • 2020-12-06 02:46

    You've perhaps got a memory leak bounded by some resource limit (RLIMIT_DATA, RLIMIT_AS?) inherited by your python script. Check your *ulimit(1)*s before you run your script, and profile the script's memory usage, as others have suggested.

    What do you do with the variable ps after the code snippet you show us? Do you keep a reference to it, never to be freed? Quoting the subprocess module docs:

    Note: The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

    ... and ps aux can be verbose on a busy system...

    Update

    You can check rlimits from with your python script using the resource module:

    import resource
    print resource.getrlimit(resource.RLIMIT_DATA) # => (soft_lim, hard_lim)
    print resource.getrlimit(resource.RLIMIT_AS)
    

    If these return "unlimited" -- (-1, -1) -- then my hypothesis is incorrect and you may move on!

    See also resource.getrusage, esp. the ru_??rss fields, which can help you to instrument for memory consumption from with the python script, without shelling out to an external program.

    0 讨论(0)
  • 2020-12-06 02:47

    If you're running a background process, chances are that you've redirected your processes stdin/stdout/stderr.

    In that case, append the option "close_fds=True" to your Popen call, which will prevent the child process from inheriting your redirected output. This may be the limit you're bumping into.

    0 讨论(0)
  • 2020-12-06 02:48

    You might want to actually wait for all of those PS processes to finish before adding swap space.

    It's not at all clear what "running as a background process executing every 60 seconds" means.

    But your call to subprocess.Popen is forking a new process each time.

    Update.

    I'd guess that you're somehow leaving all those processes running or hung in a zombie state. However, the communicate method should clean up the spawned subprocesses.

    0 讨论(0)
提交回复
热议问题