Created
August 20, 2014 11:16
-
-
Save abdullah353/8a7ff9432100d37a38a7 to your computer and use it in GitHub Desktop.
PHP multi Thread Operations
From http://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
class AsyncOperation extends Thread { | |
public function __construct($arg) { | |
$this->arg = $arg; | |
} | |
public function run() { | |
if ($this->arg) { | |
$sleep = mt_rand(1, 10); | |
printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep); | |
sleep($sleep); | |
printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg); | |
} | |
} | |
} | |
// Create a array | |
$stack = array(); | |
//Iniciate Miltiple Thread | |
foreach ( range("A", "D") as $i ) { | |
$stack[] = new AsyncOperation($i); | |
} | |
// Start The Threads | |
foreach ( $stack as $t ) { | |
$t->start(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment