Answer the question
In order to leave comments, you need to log in
How to make a beautiful scheduler for PHP script?
Good localtime!
Friends, there is such a situation:
1) a certain API with a limit of 3 requests per second
2) a large queue of requests to the API
It turns out that the script can execute more than three requests per second without any problems ...
The server is running CentOS, the service is written in PHP without frameworks.
Googled in the direction of CRON ... There you can call the script a maximum of once a minute ... So my task queue will quickly line up for months. Alternatively, you can do this:
<?
$i=0; $max=45;
while ($i<$max){
//Начало запроса к АПИ
//Конец запроса к АПИ
$i++;
sleep(1);
}
?>
Answer the question
In order to leave comments, you need to log in
A well-posed question is already half the answer. Cron is not designed for such work, you are right, it is for running a script once in n-time. For your task, you should use Gearman / Beanstalk / something else to your taste, it will be easy to balance the load and manage the number of workers there.
On loading - strongly depends on what calculations/volumes of the data, at once not to tell.
Stop! Here is something that goes wrong (it will take more than a second to process three requests or the php thread will wait a long time to execute) and get a queue block. It is necessary to limit the time with the rejection of overdue tasks. I recommend to cross php.net/manual/ru/class.splqueue.php with an asynchronous daemon (from fresh but untested: https://github.com/walkor/workerman-queue ).
If you want it simpler, then translate the limits to "requests per minute" and then you can process from the cron, as it will be possible to kill dead queue handlers from the startup script.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question