Python 使用subprocess.call会导致“调用”;“打开的文件太多”;

Python 使用subprocess.call会导致“调用”;“打开的文件太多”;,python,Python,我有以下代码来创建缩略图和保存图像。但是,在大约1000项之后,它会引发一个错误,即打开的文件太多。这是从哪里来的?我该如何修复代码 def download_file(url, extension='jpg'): """ Download a large file. Return path to saved file. """ req = requests.get(url) if not req.ok: return None gui

我有以下代码来创建缩略图和保存图像。但是,在大约1000项之后,它会引发一个错误,即
打开的文件太多
。这是从哪里来的?我该如何修复代码

def download_file(url, extension='jpg'):
    """ Download a large file.  Return path to saved file.
    """
    req = requests.get(url)
    if not req.ok:
        return None

    guid = str(uuid.uuid4())
    tmp_filename = '/tmp/%s.%s' % (guid, extension)
    with open(tmp_filename, 'w') as f:
        for chunk in req.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
                f.flush()
    return tmp_filename


def update_artwork_item(item):

    # Download the file
    tmp_filename = util.download_file(item.artwork_url)

    # Create thumbs
    THUMB_SIZES = [(1000, 120), (1000, 30)]
    guid = str(uuid.uuid4())
    S3_BASE_URL = 'https://s3-us-west-1.amazonaws.com/xxx/'

    try:

        for size in THUMB_SIZES:
            outfile = '%s_%s.jpg' % (guid, size[1])
            img = Image.open(tmp_filename).convert('RGB')
            img.thumbnail(size, Image.ANTIALIAS)
            img.save(outfile, "JPEG")
            s3_cmd = '%s %s premiere-avails --norr --public' % (S3_CMD, outfile) ## doesn't work half the time
            x = subprocess.check_call(shlex.split(s3_cmd))
            if x: raise
            subprocess.call(['rm', outfile], stdout=FNULL, stderr=subprocess.STDOUT)

    except Exception, e:

        print '&&&&&&&&&&', Exception, e

    else:
        # Save the artwork icons
        item.artwork_120 = S3_BASE_URL + guid + '_120.jpg'
        item.artwork_30 = S3_BASE_URL + guid + '_30.jpg'

        # hack to fix parallel saving
        while True:
            try:
                item.save()
            except Exception, e:
                print '******************', Exception, e
                time.sleep(random.random()*1e-1)
                continue
            else:
                subprocess.call(['rm', tmp_filename], stdout=FNULL, stderr=subprocess.STDOUT)
                break

几乎可以肯定的是,您使用的是
子流程。call
subprocess.call
是异步的,并返回一个管道对象,您负责关闭该对象。(). 因此,每次调用
subprocess.call
,都会返回一个新的管道对象,最终会耗尽文件句柄


到目前为止,最简单的方法是通过调用而不是通过管道发送到Unix
rm
命令从Python中删除文件。您可以使用check\u调用,因为check\u调用是同步的,不会返回您必须关闭的文件对象。

几乎可以肯定您使用的是
子流程。call
subprocess.call
是异步的,并返回一个管道对象,您负责关闭该对象。(). 因此,每次调用
subprocess.call
,都会返回一个新的管道对象,最终会耗尽文件句柄


到目前为止,最简单的方法是通过调用而不是通过管道发送到Unix
rm
命令从Python中删除文件。您可以使用check_调用,因为check_调用是同步的,不会返回必须关闭的文件对象。

您是否尝试在subprocess.call()之后添加img.close()?似乎有几个位置可能导致此问题:1)我建议检查
util.download_文件
,并确保它正在关闭您的http请求。2) 确保
img.save(outfile,“JPEG”)
关闭该文件。@CoryShay现在请查看其中的
download\u file()
函数的更新问题。这可能有帮助(可能是重复?):@Claudiu否,我是指文件系统实用程序,如
os.unlink
等。您是否尝试在subprocess.call()之后添加img.close()?似乎有几个位置可能导致此问题:1)我建议检查
util.download_file
,并确保它正在关闭您的http(s)请求。2) 确保
img.save(outfile,“JPEG”)
关闭文件。@CoryShay现在请查看
download\u file()
函数中的更新问题。这可能有帮助(可能是重复?):@Claudiu不,我是指文件系统实用程序,如
os.unlink
等。