3

Here's the thing, I wanted to request the API, but it sometimes took too long, so I set the timeout to 10 seconds and that worked fine for a while. But now I want to know the exact execution time for every request. I'm thinking of asynchronous requests:

$requestPromise = $client->requestAsync($this->getMethod(), $this->getEndpoint(), [
    'form_params' => $this->payload,
    'http_errors' => false,
])->then(function (Response $response) {
    Log::debug("request success:", ["response" => $response->getBody()->getContents()]);
    $this->response = $response;
});

$requestPromise->wait();

but the problem is, the main process has to wait for the response, which I don't want to. Is there a way to somehow wait no more than 10 seconds for the response and then release the main process and not to terminate the second process which is making the request itself ?

Gevorg Melkumyan
  • 628
  • 1
  • 12
  • 24
  • *"so I set the timeout to 10 seconds"* which timeout? The timeout of **your request** or the timeout of **your servers** time to response to requests? If the 1st one is the case: where is that seen in the code? *"But now I want to know the exact execution time for every request."* Could you not create a timestamp before you start the request and then once its finished create another timestamp and then [calculate the difference](https://stackoverflow.com/questions/40905174/calculate-the-difference-between-2-timestamps-in-php)? – Definitely not Rafal Mar 03 '21 at 11:09
  • *"Is there a way to somehow wait no more than 10 seconds for the response"* Have you looked at [this](https://docs.guzzlephp.org/en/stable/request-options.html#timeout)? – Definitely not Rafal Mar 03 '21 at 11:10
  • @DefinitelynotRafal the first one. I can do that, but the main PHP process will be busy waiting for the response. – Gevorg Melkumyan Mar 03 '21 at 11:21
  • *"but the main PHP process will be busy waiting for the response"* Why is that the case? Create a new timestamp before `$requestPromise = ...`, [pass the value](https://stackoverflow.com/questions/1065188/in-php-what-is-a-closure-and-why-does-it-use-the-use-identifier) and create a new timestamp right before `Log::debug(...);` and then calculate the difference? *"Is there a way to somehow wait no more than 10 seconds for the response and then release the main process"* If you need the variable outside of the function then you need to wait for it to finish using non-async requests – Definitely not Rafal Mar 03 '21 at 11:32
  • there is on_stats it can give you the execution time of each request and I do not think you areactually using aysnchronous to its full potential, also if you set timeout request options to 0 then you get unlimited time – bhucho Mar 03 '21 at 13:34

0 Answers0