Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Last active December 17, 2015 07:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krakjoe/5576666 to your computer and use it in GitHub Desktop.
Save krakjoe/5576666 to your computer and use it in GitHub Desktop.
<?php
class Job extends \Stackable {
function run() {
$this->isComplete = TRUE;
}
}
class MyWorker extends \Worker {
function run() {}
}
/*
* Container for workers and jobs
*/
class MyPool {
public $workers = [];
public $jobs = [];
/*
* Preinitialize Worker Threads
*/
public function __construct($max, $inherit = PTHREADS_INHERIT_NONE) {
if ($max) {
/* create threads */
for ($start = 0; $start < $max; $start++) {
$this->workers[
$start] = new MyWorker();
$this->workers[
$start]->start($inherit);
}
}
}
/* submit to random worker */
public function submit(Job $job){
if (count($this->workers)) {
$this->workers[
array_rand($this->workers)]->stack(
$this->jobs[]=$job);
} else $this->workers[0]->stack(
$this->jobs[]=$job);
}
/* clean pool */
public function clean() {
foreach ($this->jobs as $id => $job) {
if (is_object($job) &&
$job->isComplete) {
unset(
$this->jobs[$id]);
}
}
}
}
/* normal array */
$start = microtime(true);
/* create wrapper round multiple workers */
/* @NOTE see what happens when you raise amount of available workers ... */
$pool = new MyPool(1);
printf(
"JOB\tMem\tReal\tTime\tJPS\n");
$last = [];
for ($i=0;;) {
$pool->submit(new Job());
if (($i++%5000)==0) {
echo vsprintf("%d\t%d\t%d\t%.4f\t%d\n", $last=array(
"job" => $i,
"mem" => memory_get_usage(),
"real" => memory_get_usage(true),
"runtime" => $runtime = (microtime(true) - $start),
"jps" => ceil($i / $runtime)
));
$pool->clean();
}
}
@krakjoe
Copy link
Author

krakjoe commented May 14, 2013

ok at this point, with new changes, there's much improvement ... somehow at random points (I've seen 2 million tasks be executed sometimes, sometimes 300k), the stack still grows, so there's still a bug, but I'll get it eventually ... I will pastebin a log from an execution till it fails ...

@krakjoe
Copy link
Author

krakjoe commented May 14, 2013

pastebin: http://pastebin.com/2cLf9Z5d

got to a silly number with no sign of letting up ...

will await feedback ... is new so might be errors still ... but I think much better/faster/easier to manage memory of now ...

@krakjoe
Copy link
Author

krakjoe commented May 14, 2013

spin off to create pool of workers & show tidy in production ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment