W
W
WebDev2016-06-07 12:03:12
Laravel
WebDev, 2016-06-07 12:03:12

PHP. How to properly queue requests?

There is a database of entities, information about which needs to be updated once a day. There is an API that can be accessed no more than 1 time per second. I select all the entities and call the API in a loop, but the server responds differently, sometimes with a delay, and sometimes instantly, therefore, in order not to get a limit exceeding error, I call sleep (1) at the end of each iteration.
What tools and in what way can be done so that requests are sent exactly once a second, and if the first one is not processed to the end, then run the second one in parallel, etc.?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Greg Popov, 2016-06-07
@Gregpopov

Look towards cron

D
devian3000, 2016-06-07
@devian3000

in PHP almost nothing.
You can, in principle, immediately find a walk-around.
We take the main script, it will be the consumer in the queue.
It receives jobs from the queue.
loop (pseudocode)
while(true)
get_next_message()
shell_exec('sender.php > /dev/null');
sleep(1);
From it, once a second, we pull another script that will make a request to the API and perform any actions.
So the OS itself will create threads. and the consumer will not wait for the execution of the script actions to finish (output to /dev/null ).
You can also conjure with fork (but the fork of the script must end with 0 after useful work (request). Otherwise, an overflow will occur and the server will die.
But it's better to either put up with delays or use something that still supports streams.
Java, C++, Python, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question