Alternatives to Python Popen.communicate() memory limitations?

后端 未结 2 414
感动是毒
感动是毒 2020-12-17 09:46

I have the following chunk of Python code (running v2.7) that results in MemoryError exceptions being thrown when I work with large (several GB) files:

2条回答
  •  情歌与酒
    2020-12-17 10:21

    I think I found a solution:

    myProcess = Popen(myCmd, shell=True, stdout=PIPE, stderr=PIPE)
    for ln in myProcess.stdout:
        sys.stdout.write(ln)
    for ln in myProcess.stderr:
        sys.stderr.write(ln)
    

    This seems to get my memory usage down enough to get through the task.

    Update

    I have recently found a more flexible way of handing data streams in Python, using threads. It's interesting that Python is so poor at something that shell scripts can do so easily!

提交回复
热议问题