Skip to content

Instantly share code, notes, and snippets.

@rafaelcalleja
Created September 14, 2016 11:18
Show Gist options
  • Save rafaelcalleja/95cc51b3ad4b11255351c5855ce90e01 to your computer and use it in GitHub Desktop.
Save rafaelcalleja/95cc51b3ad4b11255351c5855ce90e01 to your computer and use it in GitHub Desktop.
Running multiple threads example php
<?php
$pids = array();
for ($i = 0; $i < 10; ++$i) {
$pid = pcntl_fork();
if ($pid != 0) {
$pids[]=$pid;
}else{
$worker = function() use($i) {
while (true) { echo "waiting $i\r\n"; sleep(1); }
};
$worker();
exit();
}
}
foreach ($pids as $pid) {
pcntl_waitpid($pid, $status);
usleep(50000);
}
/* Expected OUTPUT
waiting 0
waiting 1
waiting 2
waiting 3
waiting 4
waiting 5
waiting 6
waiting 7
waiting 8
waiting 9
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment