问题
I have code similar to what's below (it's example code). Using Guzzle, I make multiple calls to the same site to see if a document exists. If the document exists, I save it. As I make each call, the memory usage goes up. Eventually, if the number of requests is high enough, I run out of memory. I used memory_get_peak_usage
to track where the memory use was happening, and it's the Guzzle client.
The code works as expected, but I cannot find a way to tell the Guzzle Client to "reset and dump all previous requests". I'm pretty sure it's caching the results in memory, but as I've written them out to a file, I know I won't be needing said results. How do I dump them?
FWIW, my current solution is making a new client duplicating the parameters of the original one, and unsetting it periodically. It works, but it's ugly.
$client = new \Guzzle\Http\Client('some_url');
for ($i=0; $i<10000; $i++)
{
try {
$pdf = $client->get( $pdf_name )->send();
$this->filesystem->put(
$pdf_name,
$pdf->getBody( true )
);
} catch ( ClientErrorResponseException $ex ) {
}
}
回答1:
Based on a cursorary glance at the source code for the bundle the Guzzle client is making use of Doctrine's filesystem cache. References:
- Bundle/Resources/config/cache.xml
- Bundle/DependencyInjection/MisdGuzzleExtension.php
- Bundle/DependencyInjection/Configuration.php
The Bundle documentation also provides information on Caching. So, in theory to remove/disable the cache, all you have to do is remove the reference to <argument type="service" id="misd_guzzle.cache.filesystem"/>
from the the addSubscriber section of your MyBundle/Resources/config/services.xml
来源:https://stackoverflow.com/questions/29513322/how-to-reset-a-guzzle-client-and-clear-any-cached-values-memory-usage