OSError: [Errno 12] Cannot allocate memory

duncan smith duncan at invalid.invalid
Wed Nov 30 12:34:50 EST 2016


Hello,
      I have had an issue with some code for a while now, and I have not
been able to solve it. I use the subprocess module to invoke dot
(Graphviz) to generate a file. But if I do this repeatedly I end up with
an error. The following traceback is from a larger application, but it
appears to be repeated calls to 'to_image' that is the issue.


Traceback (most recent call last):
  File "<pyshell#80>", line 1, in <module>
    z = link_exp.sim1((djt, tables), variables, 1000, 400, 600,
[0,1,2,3,4,5,6], [6,7,8,9,10], ind_gens=[link_exp.males_gen()],
ind_gens_names=['Forename'], seed='duncan')
  File "link_exp.py", line 469, in sim1
    RL_F2 = EM_setup(data)
  File "link_exp.py", line 712, in full_EM
    last_g = prop.djt.g
  File "Nin.py", line 848, in draw_model
    dot_g.to_image(filename, prog='dot', format=format)
  File "dot.py", line 597, in to_image
    to_image(str(self), filename, prog, format)
  File "dot.py", line 921, in to_image
    _execute('%s -T%s -o %s' % (prog, format, filename))
  File "dot.py", line 887, in _execute
    close_fds=True)
  File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1235, in _execute_child
    self.pid = os.fork()
OSError: [Errno 12] Cannot allocate memory


The relevant (AFAICT) code is,


def to_image(text, filename, prog='dot', format='dot'):
    # prog can be a series of commands
    # like 'unflatten -l 3 | dot'
    handle, temp_path = tempfile.mkstemp()
    f = open(temp_path, 'w')
    try:
        f.write(text)
        f.close()
        progs = prog.split('|')
        progs[0] = progs[0] + ' %s ' % temp_path
        prog = '|'.join(progs)
        _execute('%s -T%s -o %s' % (prog, format, filename))
    finally:
        f.close()
        os.remove(temp_path)
        os.close(handle)

def _execute(command):
    # shell=True security hazard?
    p = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT,
                         close_fds=True)
    output = p.stdout.read()
    p.stdin.close()
    p.stdout.close()
    #p.communicate()
    if output:
        print output


Any help solving this would be appreciated. Searching around suggests
this is something to do with file handles, but my various attempts to
solve it have failed. Cheers.

Duncan



More information about the Python-list mailing list