I have a script that should run on the crown.

Briefly about the script: it takes values ​​from my database and exports them to another server using its API. The bottom line is that you cannot take all the values ​​at once, since there are so many of them, you need to gradually export them (API restrictions). You need to take them in portions of 50 pieces.

What I'm trying to do: sample 50 values, export by API, after this timeout 60 seconds, then start another one of the same process, finish the current process.

Problem:

exec('/usr/bin/wget -q --spider --tries=1 ' . $url); die('Синхронизация продолжается в фоновом режиме.');

That's how I'm trying to start another process. But the problem is that the process from which this command is called does not stop, but waits until the process started by it closes, and the one that started in turn is waiting for the one that it launched and so on.

  • What's wrong with an endless loop calling sleep (60) and breaking if necessary? - uorypm 8:43 pm
  • The question is not how to organize the export. The question is how to create a process and immediately forget about it and not wait for its implementation. - Wlodzimierz Sitdikowski
  • Your process is not running in the background, so he waits until it ends and returns the response code. You need the & at the end of the command (and, possibly, not only it) to start the process normally in the background. - etki
  • Clarifying questions: 1) exec ('/ usr / bin / wget -q --spider --tries = 1'. $ Url) - so do you run your script? 2) Do you want to run the script every 60 seconds? My guesses / answers: 1) run the script not wget, but rewrite the script for CLI and run the script as a demon. Inside the loop, condition - until the data is processed. To control the work of the script, write information about errors in a separate table and already show it in the admin panel. 2) you cannot implement multithreading due to limitations of the API of a third-party server. - Sergey

2 answers 2

I would not bother with the process, but would make a sample pool and a counter. On the counter, I would take every 60 seconds the array of data I needed and transfer via API.

PS
True, I did not quite understand what exactly needs to be transferred data or organize multithreading.

  • n = multithreading - therefore, per unit of time there will be n more requests to the API. If this is a third-party service, then it always limits the number of requests. Therefore, the speech multithreading is not. As I understand it, he wants to run the script and forget about it. - Sergey

I am not strong in php, so as an option I can offer a variant in C, a couple of functions create a completely independent process: fork (); exec ();

  • I also tried through exec, but the initial process through it waits for the child to execute. - Wlodzimierz Sitdikowski
  • He to you about functions, speaks, instead of. They differ. And what's more - he did not understand your question. - Sergey