Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PCS2020 - PHP Além do Síncrono

Diana Arnos
September 12, 2020

PCS2020 - PHP Além do Síncrono

Apresentada durante o PHP Community Summit de 2020.

Quando tudo era mato, ninguém acreditava que a linguagem PHP poderia ser usada para nada além da execução de scripts simples, sites ou um CMS de código bem bagunçado.
Então o mundo mudou e hoje temos grandes frameworks, sistemas corporativos, grandes portais e redes sociais e até sistemas de segurança e pagamentos escritos em PHP.
Agora, a comunidade se debruça sobre o novo hype (ou seria necessidade?): processamento assíncrono e paralelo.
Nessa apresentação, vamos entender a difereça entre async e paralelo, como podemos trabalhar com isso usando PHP (inclusive nativamente) e quais as principais diferenças entre as soluções mais hypadas (digo, famosas) do momento.

Diana Arnos

September 12, 2020
Tweet

More Decks by Diana Arnos

Other Decks in Programming

Transcript

  1. @dianaarnos Dev, Sec, Music, Kung Fu. PHP Engineer @ Usabilla

    (Amsterdã, Holanda) (soon) Evangelista @ PHPSP Evangelista @ PHPWomenBR
  2. PHP é síncrono Mas dá pra trabalhar assíncrono Não é

    pra isso que serve! Se funciona, então serve
  3. PHP é síncrono Mas dá pra trabalhar assíncrono Não é

    pra isso que serve! Se funciona, então serve M AS E O SHARE NOTHING?!
  4. PHP é síncrono Mas dá pra trabalhar assíncrono Não é

    pra isso que serve! Se funciona, então serve M AS E O SHARE NOTHING?! M AS É O UTRO CO NTEXTO !
  5. Paralelo Requests Tempo Task #1 Task #2 Task #3 Task

    #1 Task #3 Task #2 Task #1 Task #2 Task #3
  6. use parallel\Runtime; $runtime = new Runtime(); $future = $runtime->run(function(){ for

    ($i = 0; $i < 100; $i++) echo "*"; return "fácil"; }); for ($i = 0; $i < 100; $i++) { echo "."; } printf("\nUsar o \\parallel\\Runtime é %s\n", $future->value());
  7. Parallel x Swoole Swoole implements co-operative multi-tasking, and if you

    were following along, you already figured out that this is not based on parallel concurrency, but on asynchronous concurrency. When the readme says "you can think of co-routines as individual threads", it's referring to green threads. No user code is executed in parallel, it is executed asynchronously. The internals of swoole does use threads but to service I/O, not to execute user code. - Joe Watkins (krakjoe)
  8. <?php echo PHP_EOL . "Step 1" . PHP_EOL; for ($i

    = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Step 2" . PHP_EOL; for ($i = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Finish." . PHP_EOL;
  9. <?php echo PHP_EOL . "Step 1" . PHP_EOL; for ($i

    = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Step 2" . PHP_EOL; for ($i = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Finish." . PHP_EOL;
  10. <?php echo PHP_EOL . "Step 1" . PHP_EOL; for ($i

    = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Step 2" . PHP_EOL; for ($i = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Finish." . PHP_EOL;
  11. <?php echo PHP_EOL . "Step 1" . PHP_EOL; for ($i

    = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Step 2" . PHP_EOL; for ($i = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Finish." . PHP_EOL;
  12. <?php echo PHP_EOL . "Step 1" . PHP_EOL; for ($i

    = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Step 2" . PHP_EOL; for ($i = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Finish." . PHP_EOL;
  13. <?php echo PHP_EOL . "Step 1" . PHP_EOL; for ($i

    = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Step 2" . PHP_EOL; for ($i = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Finish." . PHP_EOL;
  14. <?php echo PHP_EOL . "Step 1" . PHP_EOL; for ($i

    = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Step 2" . PHP_EOL; for ($i = 0; $i <11; $i++) { echo "."; } echo PHP_EOL . "Finish." . PHP_EOL;
  15. <?php $function = function () { yield PHP_EOL . "Starting";

    echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end." ; };
  16. $generator = $function(); echo PHP_EOL . "Main flow"; echo $generator->current();

    echo PHP_EOL . "Main flow"; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow"; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow"; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end." ; };
  17. <?php $function = function () { yield PHP_EOL . "Starting";

    echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; }; $generator = $function(); echo PHP_EOL . "Main flow" ; echo $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ;
  18. <?php $function = function () { yield PHP_EOL . "Starting";

    echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; }; $generator = $function(); echo PHP_EOL . "Main flow" ; echo $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ;
  19. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  20. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  21. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  22. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  23. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  24. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  25. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  26. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  27. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  28. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  29. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  30. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  31. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  32. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  33. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  34. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  35. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  36. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  37. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  38. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  39. $generator = $function(); echo PHP_EOL . "Main flow" ; echo

    $generator->current(); echo PHP_EOL . "Main flow" ; $generator->next(); echo $generator->current(); $generator->next(); echo PHP_EOL . "Main flow" ; $generator->send("aeeewww"); $generator->next(); echo $generator->getReturn() . PHP_EOL; echo PHP_EOL . "Main flow" ; <?php $function = function () { yield PHP_EOL . "Starting"; echo PHP_EOL . "-"; yield PHP_EOL . "."; $incoming = yield; echo PHP_EOL . $incoming; return PHP_EOL . "Generator's end."; };
  40. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  41. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  42. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  43. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  44. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  45. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  46. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  47. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  48. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  49. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  50. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  51. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  52. $urls = ["http://dummy.restapiexample.com/api/v1/employee/1" , "http://dummy.restapiexample.com/api/v1/employee/2" ,]; $multiHandle = curl_multi_init() ;

    $curls = []; $results = []; $callback = function ($data) { echo $data . PHP_EOL; }; foreach ($urls as $url) { $handle = curl_init() ; curl_setopt_array( $handle, [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false]); $curls[$url] = $handle; curl_multi_add_handle( $multiHandle , $handle); } $isRunning = null; do { $multiCurlStatus = curl_multi_exec( $multiHandle , $isRunning); $resource = curl_multi_info_read( $multiHandle ); if ($resource) { $callback(curl_multi_getcontent( $resource['handle'])); curl_multi_remove_handle( $multiHandle , $resource['handle']); } } while ($multiCurlStatus == CURLM_CALL_MULTI_PERFORM || $isRunning);
  53. $host = "dummy.restapiexample.com"; $resource = [ "api/v1/employee/1", "api/v1/employee/2", ]; $clients

    = []; foreach ($resource as $url) { $socketClient = stream_socket_client( 'tcp://' . $host . ':80', $errno, $errstr, 10, STREAM_CLIENT_ASYNC_CONNECT ); $clients[$url] = $socketClient; $request = "GET /" . $url . " HTTP/1.1" . PHP_EOL . "Host: " . $host . PHP_EOL . PHP_EOL; fwrite($socketClient, $request); stream_set_blocking($socketClient, 0); } stream_select($clients, $write, $ex, 10); if (count($clients)) { foreach ($clients as $id => $socket) { echo fread($socket, 2000); } }
  54. $host = "dummy.restapiexample.com"; $resource = [ "api/v1/employee/1", "api/v1/employee/2", ]; $clients

    = []; foreach ($resource as $url) { $socketClient = stream_socket_client( 'tcp://' . $host . ':80', $errno, $errstr, 10, STREAM_CLIENT_ASYNC_CONNECT ); $clients[$url] = $socketClient; $request = "GET /" . $url . " HTTP/1.1" . PHP_EOL . "Host: " . $host . PHP_EOL . PHP_EOL; fwrite($socketClient, $request); stream_set_blocking($socketClient, 0); } stream_select($clients, $write, $ex, 10); if (count($clients)) { foreach ($clients as $id => $socket) { echo fread($socket, 2000); } }
  55. $host = "dummy.restapiexample.com"; $resource = [ "api/v1/employee/1", "api/v1/employee/2", ]; $clients

    = []; foreach ($resource as $url) { $socketClient = stream_socket_client( 'tcp://' . $host . ':80', $errno, $errstr, 10, STREAM_CLIENT_ASYNC_CONNECT ); $clients[$url] = $socketClient; $request = "GET /" . $url . " HTTP/1.1" . PHP_EOL . "Host: " . $host . PHP_EOL . PHP_EOL; fwrite($socketClient, $request); stream_set_blocking($socketClient, 0); } stream_select($clients, $write, $ex, 10); if (count($clients)) { foreach ($clients as $id => $socket) { echo fread($socket, 2000); } }
  56. $host = "dummy.restapiexample.com"; $resource = [ "api/v1/employee/1", "api/v1/employee/2", ]; $clients

    = []; foreach ($resource as $url) { $socketClient = stream_socket_client( 'tcp://' . $host . ':80', $errno, $errstr, 10, STREAM_CLIENT_ASYNC_CONNECT ); $clients[$url] = $socketClient; $request = "GET /" . $url . " HTTP/1.1" . PHP_EOL . "Host: " . $host . PHP_EOL . PHP_EOL; fwrite($socketClient, $request); stream_set_blocking($socketClient, 0); } stream_select($clients, $write, $ex, 10); if (count($clients)) { foreach ($clients as $id => $socket) { echo fread($socket, 2000); } }
  57. $host = "dummy.restapiexample.com"; $resource = [ "api/v1/employee/1", "api/v1/employee/2", ]; $clients

    = []; foreach ($resource as $url) { $socketClient = stream_socket_client( 'tcp://' . $host . ':80', $errno, $errstr, 10, STREAM_CLIENT_ASYNC_CONNECT ); $clients[$url] = $socketClient; $request = "GET /" . $url . " HTTP/1.1" . PHP_EOL . "Host: " . $host . PHP_EOL . PHP_EOL; fwrite($socketClient, $request); stream_set_blocking($socketClient, 0); } stream_select($clients, $write, $ex, 10); if (count($clients)) { foreach ($clients as $id => $socket) { echo fread($socket, 2000); } }
  58. $host = "dummy.restapiexample.com"; $resource = [ "api/v1/employee/1", "api/v1/employee/2", ]; $clients

    = []; foreach ($resource as $url) { $socketClient = stream_socket_client( 'tcp://' . $host . ':80', $errno, $errstr, 10, STREAM_CLIENT_ASYNC_CONNECT ); $clients[$url] = $socketClient; $request = "GET /" . $url . " HTTP/1.1" . PHP_EOL . "Host: " . $host . PHP_EOL . PHP_EOL; fwrite($socketClient, $request); stream_set_blocking($socketClient, 0); } stream_select($clients, $write, $ex, 10); if (count($clients)) { foreach ($clients as $id => $socket) { echo fread($socket, 2000); } }
  59. $host = "dummy.restapiexample.com"; $resource = [ "api/v1/employee/1", "api/v1/employee/2", ]; $clients

    = []; foreach ($resource as $url) { $socketClient = stream_socket_client( 'tcp://' . $host . ':80', $errno, $errstr, 10, STREAM_CLIENT_ASYNC_CONNECT ); $clients[$url] = $socketClient; $request = "GET /" . $url . " HTTP/1.1" . PHP_EOL . "Host: " . $host . PHP_EOL . PHP_EOL; fwrite($socketClient, $request); stream_set_blocking($socketClient, 0); } stream_select($clients, $write, $ex, 10); if (count($clients)) { foreach ($clients as $id => $socket) { echo fread($socket, 2000); } }
  60. $host = "dummy.restapiexample.com"; $resource = [ "api/v1/employee/1", "api/v1/employee/2", ]; $clients

    = []; foreach ($resource as $url) { $socketClient = stream_socket_client( 'tcp://' . $host . ':80', $errno, $errstr, 10, STREAM_CLIENT_ASYNC_CONNECT ); $clients[$url] = $socketClient; $request = "GET /" . $url . " HTTP/1.1" . PHP_EOL . "Host: " . $host . PHP_EOL . PHP_EOL; fwrite($socketClient, $request); stream_set_blocking($socketClient, 0); } stream_select($clients, $write, $ex, 10); if (count($clients)) { foreach ($clients as $id => $socket) { echo fread($socket, 2000); } }
  61. public function download () { $context = stream_context_create() ; stream_context_set_params(

    $context , ['notification' => [Downloader:: class, 'streamCallback' ]]); $handler = fopen( $this->youtube->getUrl(), 'r', false, $context ); if (!$handler ) { throw new \InvalidArgumentException( "Unable to download from link " . $this->youtube->getUrl()); } file_put_contents( $this->youtube->getName(), $handler ); } public function streamCallback ($notification_code , $severity , $message , $message_code , $bytes_transfered , $bytes_max ) { static $filesize = null; switch ($notification_code ) { case STREAM_NOTIFY_RESOLVE: case STREAM_NOTIFY_AUTH_REQUIRED: case STREAM_NOTIFY_COMPLETED: case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_AUTH_RESULT: break; case STREAM_NOTIFY_REDIRECTED: echo "Being redirected to: " . $message . PHP_EOL; break; case STREAM_NOTIFY_CONNECT: echo "Connected!" . PHP_EOL; case STREAM_NOTIFY_FILE_SIZE_IS: $filesize = $bytes_max ; echo "Filesize: " . $filesize . PHP_EOL; case STREAM_NOTIFY_MIME_TYPE_IS: echo "Mime-type: " . $message . PHP_EOL; case STREAM_NOTIFY_PROGRESS: if ($bytes_transfered > 0) { $length = (int)(($bytes_transfered / $filesize ) * 100); printf("\r[%-100s] %d%% (%2d/%2d mb)" , str_repeat( "=", $length) . ">", $length, ($bytes_transfered / 1024 / 1024), $filesize / 1024 / 1024); } break; } }
  62. public function download () { $context = stream_context_create() ; stream_context_set_params(

    $context , ['notification' => [Downloader:: class, 'streamCallback' ]]); $handler = fopen( $this->youtube->getUrl(), 'r', false, $context ); if (!$handler ) { throw new \InvalidArgumentException( "Unable to download from link " . $this->youtube->getUrl()); } file_put_contents( $this->youtube->getName(), $handler ); } public function streamCallback ($notification_code , $severity , $message , $message_code , $bytes_transfered , $bytes_max ) { static $filesize = null; switch ($notification_code ) { case STREAM_NOTIFY_RESOLVE: case STREAM_NOTIFY_AUTH_REQUIRED: case STREAM_NOTIFY_COMPLETED: case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_AUTH_RESULT: break; case STREAM_NOTIFY_REDIRECTED: echo "Being redirected to: " . $message . PHP_EOL; break; case STREAM_NOTIFY_CONNECT: echo "Connected!" . PHP_EOL; case STREAM_NOTIFY_FILE_SIZE_IS: $filesize = $bytes_max ; echo "Filesize: " . $filesize . PHP_EOL; case STREAM_NOTIFY_MIME_TYPE_IS: echo "Mime-type: " . $message . PHP_EOL; case STREAM_NOTIFY_PROGRESS: if ($bytes_transfered > 0) { $length = (int)(($bytes_transfered / $filesize ) * 100); printf("\r[%-100s] %d%% (%2d/%2d mb)" , str_repeat( "=", $length) . ">", $length, ($bytes_transfered / 1024 / 1024), $filesize / 1024 / 1024); } break; } }
  63. public function download () { $context = stream_context_create() ; stream_context_set_params(

    $context , ['notification' => [Downloader:: class, 'streamCallback' ]]); $handler = fopen( $this->youtube->getUrl(), 'r', false, $context ); if (!$handler ) { throw new \InvalidArgumentException( "Unable to download from link " . $this->youtube->getUrl()); } file_put_contents( $this->youtube->getName(), $handler ); } public function streamCallback ($notification_code , $severity , $message , $message_code , $bytes_transfered , $bytes_max ) { static $filesize = null; switch ($notification_code ) { case STREAM_NOTIFY_RESOLVE: case STREAM_NOTIFY_AUTH_REQUIRED: case STREAM_NOTIFY_COMPLETED: case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_AUTH_RESULT: break; case STREAM_NOTIFY_REDIRECTED: echo "Being redirected to: " . $message . PHP_EOL; break; case STREAM_NOTIFY_CONNECT: echo "Connected!" . PHP_EOL; case STREAM_NOTIFY_FILE_SIZE_IS: $filesize = $bytes_max ; echo "Filesize: " . $filesize . PHP_EOL; case STREAM_NOTIFY_MIME_TYPE_IS: echo "Mime-type: " . $message . PHP_EOL; case STREAM_NOTIFY_PROGRESS: if ($bytes_transfered > 0) { $length = (int)(($bytes_transfered / $filesize ) * 100); printf("\r[%-100s] %d%% (%2d/%2d mb)" , str_repeat( "=", $length) . ">", $length, ($bytes_transfered / 1024 / 1024), $filesize / 1024 / 1024); } break; } }
  64. public function download () { $context = stream_context_create() ; stream_context_set_params(

    $context , ['notification' => [Downloader:: class, 'streamCallback' ]]); $handler = fopen( $this->youtube->getUrl(), 'r', false, $context ); if (!$handler ) { throw new \InvalidArgumentException( "Unable to download from link " . $this->youtube->getUrl()); } file_put_contents( $this->youtube->getName(), $handler ); } public function streamCallback ($notification_code , $severity , $message , $message_code , $bytes_transfered , $bytes_max ) { static $filesize = null; switch ($notification_code ) { case STREAM_NOTIFY_RESOLVE: case STREAM_NOTIFY_AUTH_REQUIRED: case STREAM_NOTIFY_COMPLETED: case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_AUTH_RESULT: break; case STREAM_NOTIFY_REDIRECTED: echo "Being redirected to: " . $message . PHP_EOL; break; case STREAM_NOTIFY_CONNECT: echo "Connected!" . PHP_EOL; case STREAM_NOTIFY_FILE_SIZE_IS: $filesize = $bytes_max ; echo "Filesize: " . $filesize . PHP_EOL; case STREAM_NOTIFY_MIME_TYPE_IS: echo "Mime-type: " . $message . PHP_EOL; case STREAM_NOTIFY_PROGRESS: if ($bytes_transfered > 0) { $length = (int)(($bytes_transfered / $filesize ) * 100); printf("\r[%-100s] %d%% (%2d/%2d mb)" , str_repeat( "=", $length) . ">", $length, ($bytes_transfered / 1024 / 1024), $filesize / 1024 / 1024); } break; } }
  65. public function download () { $context = stream_context_create() ; stream_context_set_params(

    $context , ['notification' => [Downloader:: class, 'streamCallback' ]]); $handler = fopen( $this->youtube->getUrl(), 'r', false, $context ); if (!$handler ) { throw new \InvalidArgumentException( "Unable to download from link " . $this->youtube->getUrl()); } file_put_contents( $this->youtube->getName(), $handler ); } public function streamCallback ($notification_code , $severity , $message , $message_code , $bytes_transfered , $bytes_max ) { static $filesize = null; switch ($notification_code ) { case STREAM_NOTIFY_RESOLVE: case STREAM_NOTIFY_AUTH_REQUIRED: case STREAM_NOTIFY_COMPLETED: case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_AUTH_RESULT: break; case STREAM_NOTIFY_REDIRECTED: echo "Being redirected to: " . $message . PHP_EOL; break; case STREAM_NOTIFY_CONNECT: echo "Connected!" . PHP_EOL; case STREAM_NOTIFY_FILE_SIZE_IS: $filesize = $bytes_max ; echo "Filesize: " . $filesize . PHP_EOL; case STREAM_NOTIFY_MIME_TYPE_IS: echo "Mime-type: " . $message . PHP_EOL; case STREAM_NOTIFY_PROGRESS: if ($bytes_transfered > 0) { $length = (int)(($bytes_transfered / $filesize ) * 100); printf("\r[%-100s] %d%% (%2d/%2d mb)" , str_repeat( "=", $length) . ">", $length, ($bytes_transfered / 1024 / 1024), $filesize / 1024 / 1024); } break; } }
  66. <?php $pid = pcntl_fork() ; if ($pid == -1) {

    die('could not fork' ); } else if ($pid) { // we are the parent pcntl_wait($status); //Protect against Zombie children } else { // we are the child }