-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Results not output in execution time order? [question] #20
Comments
Maybe because in multy thread mode there no order for running process? They complete in parallel, not one by one. |
Ditto the above results. use BR_MODS\Logger;
use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait;
use Spork\Fork;
use Spork\ProcessManager;
class ThreadTest implements EventEmitterInterface{
/**
* Implements EventEmitterInterface
*
* @uses \Evenement\EventEmitterTrait
*/
use EventEmitterTrait;
public function __construct() {
$this->count = 0;
$this->on('log', function($log_msg) {
Logger::info(null, $log_msg);
});
}
public function doWork( ProcessManager $manager, $i) {
$curTime = microtime(true);
Logger::info(null, "Starting: $i");
$fork_deferred = $manager->fork(function( \Spork\SharedMemory $sharedMemory) use ($i, $curTime) {
Logger::info(null, 'Greeting from '.$i. ' : '. getmypid());
$rand2 = rand(1, 5);
if($i == 4) {
$rand2 = 9;
}
sleep ( $rand2);
$time_diff = round(microtime(true) - $curTime,3);
Logger::info(null, 'Finished from '.$i. ' : '. getmypid());
$res = 'Hello from '.getmypid() . ' with $i: '.$i. ' and $rand2 '. $rand2. ' finish Time: '. date("h:i:s") . ' diff time: '. $time_diff ;
return $res;
}
);
return $fork_deferred;
}
public function go() {
$manager = new ProcessManager(null, null, true);
for($i=1;$i<=15;$i++){
$fork_deferred = $this->doWork($manager, $i);
$fork_deferred->then(function ( Fork $fork) use ($i) {
$res = $fork->getResult();
$this->emit('log', array("Done: $res"));
});
}
}
}
$time_start = microtime(true);
$Thread = new ThreadTest();
$Thread->go();
//\BR_MODS\LoopMgrModel::run();
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo 'Total Execution Time: '.$execution_time.' Seconds'. "\n"; output is as follows 2015-09-19T13:31:21.7626200 info Starting: 1 Threads all terminate in the order one would expect, but the final output arrives in the order in which the threads were started. Can anyone explain this result and how to get ->then( to fire as a thread ends? pbowyer, did you get to the bottom of this? Regards, Darren |
Thanks for writing this library Kris. I have a question: for the code samples below (doing the same thing in two different ways) why does the output appear in the order of the input, not in the order the processes finished? Can I change that?
I'm trying to speed up calling webservices by calling them in parallel and returning results to the client (via HTTP) as quickly as possible. Running them in parallel and getting all results back at once is an improvement over the current serial pipeline, but I don't understand why they're not in order of execution time?
or
The text was updated successfully, but these errors were encountered: