Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous sleep #71

Closed
sminnee opened this issue Jul 18, 2017 · 6 comments
Closed

Asynchronous sleep #71

sminnee opened this issue Jul 18, 2017 · 6 comments

Comments

@sminnee
Copy link

sminnee commented Jul 18, 2017

I was building some async code that triggers a delayed retry in the case of an error, similar to:

$promise = $client->getAsync(...);

$promise->then(
  function ($result) { ... },
  function ($error) {
    sleep(5); 
    $client->getAsync(...);
  }
);

Of course, sleep(5) is blocking. Is there an asynchronous equivalent for sleep, such as a promise that will resolve within a wait() call after the given amount of time? If not, can one be added to the library?

@alexeyshockov
Copy link
Collaborator

There is no concurrent version of sleep() in Guzzle Promises out of the box. You can take a look at a library (I'm the developer) which integrates Guzzle with React PHP. With React you can use timers to create "sleep promises" (see sleep_fn).

But if you need just delayed retries, try to use the default retry middleware. Also a usage example.

@mtdowling
Copy link
Member

Guzzle (the client) also supports the delay request option which can be used to delay the sending of a request: http://docs.guzzlephp.org/en/stable/request-options.html#delay.

@sminnee
Copy link
Author

sminnee commented Jul 20, 2017

Thanks for the advice. I'll check out the React bridge.

The default retry middleware, uses the Guzzle delay option, which uses a `usleep() call. My understanding is that this would block other requests from being processed in the meantime.

My use-case is a domain scanner, so efficient parallel processing is quite important for its overall performance.

@sminnee sminnee closed this as completed Jul 20, 2017
@mtdowling
Copy link
Member

Only the blocking handlers use usleep. The CurlMultiHandler does not.

@abiusx
Copy link

abiusx commented Jul 14, 2019

You can use GuzzleHttp to send a request to a non-existing server with a certain delay. This can work as an async sleep.

@elcreator
Copy link

Only the blocking handlers use usleep. The CurlMultiHandler does not.

CurlMultiHandler uses usleep too: https://github.com/guzzle/guzzle/blob/7.8/src/Handler/CurlMultiHandler.php#L187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants