根据请求“完成",文件未关闭使用食尸鬼5.3 [英] File not closed on Request "Complete" using guzzle 5.3

查看:43
本文介绍了根据请求“完成",文件未关闭使用食尸鬼5.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用枪口池上传1500多个文件.由于我们不想遇到打开的文件过多"的情况,因此我们认为可以使用事件之前"执行打开操作,使用事件完成"来关闭流. PHP无法有效关闭资源(我们打了太多打开的文件).知道发生了什么/我们可以做什么来解决此问题?

We are uploading 1500+ files using a guzzle pool. Since we don't want to run into "too many open files" we figured we could use the event "before" to do the fopen and the "complete" to fclose the stream. PHP is not effectively closing the resource (and we hit the too many open files). Any idea what is happening / What we can do to fix this issue?

代码如下:

    $client = new GuzzleHttp\Client();
    $requests = [];
    foreach($files as $fileName) {
        $options = [
            'debug' => false,
            'events' => [
                'before'=>
                    function (BeforeEvent $e) use ($fileName) {
                        echo 'Opening body|'.count(glob('/proc/'.posix_getpid().'/fd/*')).PHP_EOL;
                        $stream = \GuzzleHttp\Stream\Stream::factory(fopen($fileName,'r'));
                        $e->getRequest()->setBody($stream);
                    },
                'complete' =>
                    function (CompleteEvent $e){
                        echo 'Closing body|'.count(glob('/proc/'.posix_getpid().'/fd/*')).PHP_EOL;
                        $stream = $e->getResponse()->getBody();
                        $stream->close();
                    },
            ]
        ];

        $request = $client->createRequest('POST', $this->baseUri . $this->uploadPath, $options);
        $requests[] = $request;
    }
    Pool::batch($client, $requests, ['pool_size'=> $this->poolSize]);

输出: Opening body|31 Closing body|57 Opening body|57 Opening body|58 Closing body|59 Opening body|59 Closing body|61 Opening body|61 Closing body|62 Opening body|62 Closing body|63 ...

Output : Opening body|31 Closing body|57 Opening body|57 Opening body|58 Closing body|59 Opening body|59 Closing body|61 Opening body|61 Closing body|62 Opening body|62 Closing body|63 ...

这个数字永远不会减少.

The number never goes down.

推荐答案

您需要关闭请求的流而不是响应流.响应流似乎已自动关闭.所以只需替换

You need to close the request's stream instead of the response one. The response stream seems to be closed automatically. So just replace

$stream = $e->getResponse()->getBody();

通过

$stream = $e->getRequest()->getBody();

在您的完整活动中

这篇关于根据请求“完成",文件未关闭使用食尸鬼5.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆