The code has the following snippet:

do { curl_multi_exec($curlMultiHandle, $running); } while($running > 0); 

As far as I understand, this is a cycle that is executed until answers are received from all sources.

Actually, the question is - how much will it load the processor in case there is no answer for a long time?

If the load is still large, is it possible to use usleep? I remember that in C or Pascal, when using such a function, the OS could switch to a different process for a specified time, respectively, MY process will not load the processor. Can this be done in PHP?

    1 answer 1

    Such a call will dispose of a single processor core almost completely. At that, only one only because PHP is single-threaded.

    Yes, you can plug in usleep and this will fix the situation with CPU utilization. But it would be better to pull the special waiting function curl_multi_select . That's just in the comments to it are referred to as already introduced bugs in the behavior and simply on the features of the implementation in libcurl . In general, it is optimal to use both usleep and curl_multi_select .

      // отправляСм запросы $active = null; do { $mrc = curl_multi_exec($multi, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { // ΠΆΠ΄Ρ‘ΠΌ ΠΊΠ°ΠΊΡƒΡŽ-Π½ΠΈΠ±ΡƒΠ΄ΡŒ Π°ΠΊΡ‚ΠΈΠ²Π½ΠΎΡΡ‚ΡŒ ΠΎΡ‚ ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ² if (curl_multi_select($multi) == -1) { usleep(100); } // ΠΎΠΏΡ€Π°ΡˆΠΈΠ²Π°Π΅ΠΌ curl_multi_exec Π½Π° ΠΏΡ€Π΅Π΄ΠΌΠ΅Ρ‚, Π΅ΡΡ‚ΡŒ Π»ΠΈ Π΅Ρ‰Ρ‘ Π°ΠΊΡ‚ΠΈΠ²Π½Ρ‹Π΅ ΠΏΠΎΡ‚ΠΎΠΊΠΈ do { $mrc = curl_multi_exec($multi, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } 

    The usleep value here can be selected for a specific project. Documentation for libcurl recommends starting with 100 milliseconds, then you can test in your environment with your tasks and decrease-increase the value.