Ejemplo n.º 1
0
 /**
  * worker master - Pool
  */
 function workerPool()
 {
     $start = microtime(true);
     echo "\nworker start at {$start}";
     $jobs = array();
     if ($jobs) {
         $wNum = 5;
         //进程数
         $jobCnt = count($jobs);
         $w = ceil($jobCnt / $wNum);
         //
         $pool = new Pool("demoWorker", $w);
         $pool->addJobs($jobs);
         $pool->waiting();
         $stop = microtime(true);
         $cost = $stop - $start;
         echo "\nworker {$jobCnt} jobs done at {$stop}";
         echo "\nworker cost time = {$cost}";
     } else {
         //for daemon mode
         echo "\nworker no job and sleep 5s";
         sleep(5);
     }
     $end = microtime(true);
     echo "\nworker end at {$end}";
 }
Ejemplo n.º 2
0
 protected function assertHit($expected, Pool $pool, $key)
 {
     $this->assertEquals($expected, $pool->get($key));
     $this->assertEquals($expected, $pool->getItem($key)->get());
     $this->assertTrue($pool->getItem($key)->isHit());
     $this->assertTrue($pool->hasItem($key));
 }
Ejemplo n.º 3
0
 /**
  * Called when application instance is going to shutdown.
  * @return boolean Ready to shutdown?
  */
 public function onShutdown($graceful = false)
 {
     if ($this->pool) {
         return $this->pool->onShutdown($graceful);
     }
     return true;
 }
Ejemplo n.º 4
0
 /**
  * Called when the worker is ready to go
  * @return void
  */
 public function onReady()
 {
     $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance();
     $this->redis->multi(function ($multi) {
         // "OK"
         D('start multi: ' . $multi->result);
         $multi->set('test1', 'value1', function ($redis) use($multi) {
             // "QUEUED"
             D('in multi 1: ' . $redis->result);
             $this->redis->set('test1', 'value1-new', function ($redis) {
                 // "OK", not "QUEUED"
                 D('out multi 1: ' . $redis->result);
             });
             setTimeout(function ($timer) use($multi) {
                 // "QUEUED"
                 $multi->set('test2', 'value2', function ($redis) use($multi) {
                     D('in multi 2: ' . $redis->result);
                     $multi->exec(function ($redis) {
                         D('exec');
                         D($redis->result);
                     });
                 });
                 $timer->free();
             }, 200000.0);
         });
     });
 }
Ejemplo n.º 5
0
 public function testPoolGc()
 {
     $pool = new Pool(1, PoolTestWorker::class, [new stdClass(), new Threaded()]);
     $work = new PoolTestWork();
     $pool->submit($work);
     while (@$i++ < 2) {
         $pool->submit(new PoolTestWork());
         # nothing to assert, no exceptions please
     }
     $pool->submitTo(0, new PoolTestWork());
     # nothing to assert, no exceptions please
     /* synchronize with pool */
     $sync = new PoolTestSync();
     $pool->submit($sync);
     $sync->synchronized(function ($sync) {
         if (!$sync->finished) {
             $sync->wait();
         }
     }, $sync);
     $pool->collect(function ($task) {
         $this->assertTrue($task->isGarbage());
         return true;
     });
     $pool->shutdown();
 }
Ejemplo n.º 6
0
 public function useMultiCore1()
 {
     $core = $this->getCoreCount();
     $pool = new \Pool($core);
     for ($i = 0; $i < $core; $i++) {
         $pool->submit(new ProveWorker());
     }
 }
Ejemplo n.º 7
0
 /**
  * @param string[] $adjustments
  * @return AdjustmentInterface[]
  */
 protected function fetchAdjustments($adjustments)
 {
     $instances = [];
     foreach ($adjustments as $code) {
         $instances[$code] = $this->adjustmentPool->getAdjustmentByCode($code);
     }
     uasort($instances, [$this, 'sortAdjustments']);
     return $instances;
 }
Ejemplo n.º 8
0
 function allAction()
 {
     $order = addslashes(trim($this->_request->getParam('order', 'Name')));
     $this->view->title = $this->view->translate->_("Pools");
     // to view
     $this->view->meta_refresh = 300;
     // meta http-equiv="refresh"
     $pools = new Pool();
     $this->view->pools = $pools->fetchAll(null, $order);
 }
Ejemplo n.º 9
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $pool = new \Pool($input->getOption('threads'), \Worker::class);
     foreach ($input->getArgument('indexes') as $index) {
         $pool->submit(new IndexerRunner($input->getOption('indexer'), $input->getOption('config'), $index));
     }
     $pool->shutdown();
     $pool->collect(function (IndexerRunner $work) use($output) {
         $output->writeln($work->return);
     });
 }
Ejemplo n.º 10
0
 public function __construct($threadSayisi, BurtayThread $sinif)
 {
     $pool = new Pool($threadSayisi);
     $is_listesi = array();
     for ($i = 1; $i <= $threadSayisi; $i++) {
         $is_listesi[] = new burtayThreadPool($sinif);
     }
     foreach ($is_listesi as $liste) {
         $pool->submit($liste);
     }
     $pool->shutdown();
 }
Ejemplo n.º 11
0
 /**
  * @param Pool $pool
  *
  * @return array|bool
  */
 private function shuffleTillMatch(Pool $pool)
 {
     $timeToStop = microtime(true) + self::SHUFFLE_TIME_LIMIT;
     $entries = $pool->getEntries()->getValues();
     while (microtime(true) < $timeToStop) {
         $set = $this->shuffleArray($entries);
         if ($this->checkValidMatch($entries, $set)) {
             $this->matchedExcludes = $set;
             return $set;
         }
     }
     return false;
 }
Ejemplo n.º 12
0
 public function testUnsubscribeAll()
 {
     $pool = new Pool();
     $mockEvent1 = $this->getMockBuilder('Athrois\\Event')->getMock();
     $mockEvent2 = $this->getMockBuilder('AnotherEvent')->getMock();
     $mockListener = $this->getMockBuilder('Athrois\\Listener')->getMock();
     $identifier1 = Pool::getId($mockEvent1);
     $identifier2 = Pool::getId($mockEvent2);
     $pool->register($mockListener, $identifier1);
     $pool->register($mockListener, $identifier2);
     $pool->unsubscribe($mockListener);
     $this->assertAttributeEquals(array(), 'listeners', $pool);
 }
Ejemplo n.º 13
0
 /**
  * Called when the worker is ready to go.
  * @return void
  */
 public function onReady()
 {
     $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance();
     /*$this->redis->eval("return {'a','b','c', {'d','e','f', {'g','h','i'}} }",0, function($redis) {
     			Daemon::log(Debug::dump($redis->result));
     		});*/
     $this->redis->subscribe('te3st', function ($redis) {
         Daemon::log(Debug::dump($redis->result));
     });
     $this->redis->psubscribe('test*', function ($redis) {
         Daemon::log(Debug::dump($redis->result));
     });
 }
Ejemplo n.º 14
0
 public function setPool()
 {
     $pool = new Pool();
     foreach ($this->getConfiguration() as $key => $value) {
         $array = new ArrayObject($value);
         $server = new Server();
         $server->setHost($array->offsetGet(self::SERVER_PROPERTY_HOST))->setPort($array->offsetGet(self::SERVER_PROPERTY_PORT))->setName($key);
         if ($array->offsetExists(self::SERVER_PROPERTY_AUTH)) {
             $server->setAuth($array->offsetGet(self::SERVER_PROPERTY_AUTH));
         }
         $pool->attach($server);
     }
     $this->pool = $pool;
 }
Ejemplo n.º 15
0
 /**
  * @param int $currentTick
  */
 public function mainThreadHeartbeat($currentTick)
 {
     $this->currentTick = $currentTick;
     while ($this->isReady($this->currentTick)) {
         /** @var TaskHandler $task */
         $task = $this->queue->extract();
         if ($task->isCancelled()) {
             unset($this->tasks[$task->getTaskId()]);
             continue;
         } else {
             $task->timings->startTiming();
             $task->run($this->currentTick);
             $task->timings->stopTiming();
         }
         if ($task->isRepeating()) {
             $task->setNextRun($this->currentTick + $task->getPeriod());
             $this->queue->insert($task, $this->currentTick + $task->getPeriod());
         } else {
             $task->remove();
             unset($this->tasks[$task->getTaskId()]);
         }
     }
     if ($this->asyncTasks > 0) {
         //Garbage collector
         $this->asyncPool->collect([$this, "collectAsyncTask"]);
         foreach ($this->asyncTaskStorage as $asyncTask) {
             if ($asyncTask->isFinished() and !$asyncTask->isCompleted()) {
                 $this->collectAsyncTask($asyncTask);
             }
         }
     }
 }
Ejemplo n.º 16
0
 private function testPool()
 {
     $initTime = microtime(true);
     $pool = new \Pool(8, \Worker::class);
     for ($i = 0; $i < 10; $i++) {
         $pool->submit(new Work());
     }
     $initTime = microtime(true) - $initTime;
     $finishTime = microtime(true);
     $pool->shutdown();
     $pool->collect(function (Work $work) {
         echo 'Work of ', get_class($work), ' #', $work->i, ' - ', $work->getWork(), PHP_EOL;
     });
     $finishTime = microtime(true) - $finishTime;
     return [$initTime, $finishTime];
 }
Ejemplo n.º 17
0
 public function definePool($name, $num = null)
 {
     $parts = explode(':', $name);
     $alias = $parts[0];
     if (count($parts) > 1) {
         $class = $parts[1];
     } else {
         $class = $parts[0];
     }
     if (!$class) {
         $class = $alias;
     }
     $this->pools[$alias] = $pool = new Pool(new Definition($class), $num);
     $pool->setFactory($this);
     return $pool->getDefinition();
 }
Ejemplo n.º 18
0
 public function get_dataksmurni($date = false)
 {
     if (!$date) {
         $date = date('Y-m-d');
     }
     $month = date('n', strtotime($date));
     $year = date('Y', strtotime($date));
     $series = array();
     $categories = array();
     foreach (Pool::all() as $pool) {
         $datafianan = array();
         for ($j = 1; $j <= date('t', strtotime($date)); $j++) {
             $categories[] = $j;
             $datafianan[] = 0;
         }
         $reportdaily = DB::table('financial_report_graf_ks_detail')->where_month($month)->where_year($year)->order_by('operasi_time', 'asc')->where_pool_id($pool->id)->where_in('operasi_status_id', array(1, 3))->where('setoran_cash', '<', 50000000)->group_by('operasi_time')->get(array('operasi_time', DB::raw('sum(ks) as subks')));
         $arraydata = array();
         foreach ($reportdaily as $finan) {
             $potition = date('j', strtotime($finan->operasi_time)) - 1;
             $arraydata[$potition] = (int) $finan->subks;
         }
         $datas = array_replace($datafianan, $arraydata);
         $series[] = array('name' => $pool->pool_name, 'data' => $datas);
     }
     $returndata = array('categories' => $categories, 'series' => $series);
     return json_encode($returndata);
 }
Ejemplo n.º 19
0
 public function get_add()
 {
     $this->data['pools'] = Koki::to_dropdown(Pool::all(), 'id', 'pool_name', array(0 => 'All'));
     $this->data['users'] = Koki::to_dropdown(User::all(), 'id', 'first_name');
     $this->data['create'] = true;
     return View::make('themes.modul.' . $this->views . '.form', $this->data);
 }
Ejemplo n.º 20
0
 public static function wrap($coroutine)
 {
     if ($coroutine instanceof \Generator) {
         $coKey = $coroutine->key();
         $coValue = $coroutine->current();
         if ($coKey && in_array($coKey, self::$_ioQueue)) {
             try {
                 $client = \Pool::get($coKey, $coValue);
                 if ($client) {
                     $client->setCoroutine($coroutine);
                     $coroutine->send($client);
                 } else {
                     Queue::push($coKey, $coroutine);
                 }
             } catch (exception $e) {
                 $coroutine->throw($e);
             }
         } else {
             if ($coValue instanceof \Coroutine\Base) {
                 $coValue->setCoroutine($coroutine);
             }
         }
     }
     return $coroutine;
 }
Ejemplo n.º 21
0
 public function get_datamonthly($date = false)
 {
     if (!$date) {
         $date = date('Y-m-d');
     }
     $year = date('Y', strtotime($date));
     $series = array();
     $categories = array();
     foreach (Pool::all() as $pool) {
         $datafianan = array();
         for ($j = 1; $j <= 12; $j++) {
             $categories[] = $j;
             $datafianan[] = 0;
         }
         $reportmonthly = DB::table('financial_report_summary_month_graf')->where_year($year)->order_by('operasi_time', 'asc')->where_pool_id($pool->id)->where('setoran_cash', '<', 993000000)->get();
         $arraydata = array();
         $bulan = 0;
         foreach ($reportmonthly as $finan) {
             $potition = $finan->month;
             $arraydata[$potition] = (int) ($finan->setoran_cash - ($finan->biaya_cuci + $finan->iuran_laka));
         }
         $datas = array_replace($datafianan, $arraydata);
         $series[] = array('name' => $pool->pool_name, 'data' => $datas);
     }
     $returndata = array('categories' => $categories, 'series' => $series);
     return json_encode($returndata);
 }
Ejemplo n.º 22
0
 public function run() : array
 {
     $success = [];
     $failed = [];
     $client = new Client(['timeout' => $this->timeout]);
     $pool = new Pool($client, $this->requests(), ['concurrency' => count($this->urls), 'fulfilled' => function (Response $response, string $url) use(&$success, &$failed) {
         if (false !== strpos((string) $response->getBody(), $this->needle)) {
             $success[] = $url;
         } else {
             $failed[] = $url;
         }
     }]);
     $pool->promise()->wait();
     $failed = array_merge($failed, array_diff($this->urls, $success, $failed));
     return [$success, $failed];
 }
Ejemplo n.º 23
0
    /**
     * Returns the global worker pool for the current context.
     *
     * @param PoolInterface|null $pool A worker pool instance.
     *
     * @return PoolInterface The global worker pool instance.
     */
    function pool(PoolInterface $pool = null)
    {
        static $instance;

        if (null !== $pool) {
            $instance = $pool;
        } elseif (null === $instance) {
            $instance = new Pool();
        }

        if (!$instance->isRunning()) {
            $instance->start();
        }

        return $instance;
    }
Ejemplo n.º 24
0
 /**
  * @param Pool $pool
  * @return array|bool
  */
 private function permutateTillMatch(Pool $pool)
 {
     $entries = $pool->getEntries()->getValues();
     $set = $this->shuffleArray($entries);
     $size = count($set) - 1;
     $perm = range(0, $size);
     do {
         $shuffled = array();
         foreach ($perm as $i) {
             $shuffled[] = $set[$i];
         }
         if ($this->checkValidMatch($entries, $shuffled)) {
             return $shuffled;
         }
     } while ($perm = $this->nextPermutation($perm, $size));
     return false;
 }
Ejemplo n.º 25
0
 public static function Init()
 {
     $config = \Rds\Configuration::get();
     $boot = \Rds\Bootstrap::getInstace($config);
     $totalPages = $boot->getTotalPages();
     if ($totalPages > 0) {
         $pool = new \Pool($config["app"]["workers"], \Rds\Worker::class, array("Vendor/autoload.php", new \Rds\StackableConfig($config)));
         for ($page = 1; $page <= $totalPages; $page++) {
             $task = new \Rds\Task($page);
             $pool->submit($task);
         }
         $pool->shutdown();
         $pool->collect(function ($work) {
             return $work->isGarbage();
         });
     }
 }
Ejemplo n.º 26
0
 /**
  *      * {@inheritdoc}
  *           */
 public function prePersist($object)
 {
     $user = $this->getConfigurationPool()->getContainer()->get('security.context')->getToken()->getUser();
     $object->setUser($user);
     if ($object->getRawContent()) {
         $object->setContent($this->formatterPool->transform($object->getContentFormatter(), $object->getRawContent()));
     }
     return $object;
 }
Ejemplo n.º 27
0
 public function collectTasks()
 {
     Timings::$schedulerAsyncTimer->startTiming();
     for ($i = 0; $i < 2; $i++) {
         if (!$this->pool->collect(function (AsyncTask $task) {
             if ($task->isGarbage() and !$task->isRunning() and !$task->isCrashed()) {
                 if (!$task->hasCancelledRun()) {
                     $task->onCompletion($this->server);
                 }
                 $this->removeTask($task);
             } elseif ($task->isTerminated() or $task->isCrashed()) {
                 $this->server->getLogger()->critical("Could not execute asynchronous task " . (new \ReflectionClass($task))->getShortName() . ": Task crashed");
                 $this->removeTask($task);
             }
             return $task->isGarbage();
         })) {
             break;
         }
     }
     Timings::$schedulerAsyncTimer->stopTiming();
 }
Ejemplo n.º 28
0
 /**
  * Called when the worker is ready to go
  * @return void
  */
 public function onReady()
 {
     $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance();
     $this->redis->hmset('myset', 'field1', 'value1', 'field2', 'value2', 'field3', 'value3', 'field4', 'value4', 'field5', 'value5', function ($redis) {
         $this->redis->hgetall('myset', function ($redis) {
             D('TEST 1: HGETALL');
             foreach ($redis as $key => $value) {
                 D($key . ' - ' . $value);
             }
             D($redis->assoc);
         });
         $this->redis->hmget('myset', 'field2', 'field4', function ($redis) {
             D('TEST 2: HMGET');
             foreach ($redis as $key => $value) {
                 D($key . ' - ' . $value);
             }
             D($redis->assoc);
         });
     });
     $this->redis->zadd('myzset', 100, 'one', 150, 'two', 325, 'three', function ($redis) {
         $this->redis->zrange('myzset', 0, -1, function ($redis) {
             D('TEST 3: ZRANGE');
             foreach ($redis as $key => $value) {
                 D($key . ' - ' . $value);
             }
             D($redis->assoc);
         });
         $this->redis->zrange('myzset', 0, -1, 'WITHSCORES', function ($redis) {
             D('TEST 4: ZRANGE WITHSCORES');
             foreach ($redis as $key => $value) {
                 D($key . ' - ' . $value);
             }
             D($redis->assoc);
         });
     });
     $this->redis->subscribe('mysub', function ($redis) {
         D('TEST 5: SUB & PUB');
         foreach ($redis as $key => $value) {
             D($key . ' - ' . $value);
         }
         D($redis->channel, $redis->msg, $redis->assoc);
     });
     $this->redis->publish('mysub', 'Test message!');
 }
Ejemplo n.º 29
0
 /**
  * Called when the worker is ready to go
  * @return void
  */
 public function onReady()
 {
     $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance();
     $params = [];
     foreach (range(0, 100) as $i) {
         $params[] = 'myset' . $i;
         $params[] = 'value' . $i;
     }
     $params[] = function ($redis) {
         $params = [function ($redis) {
             D('Count: ' . count($redis->result[1]) . '; Next: ' . $redis->result[0]);
         }];
         $cbEnd = function ($redis, $scan) {
             D('Full scan end!');
         };
         // test 1
         // call_user_func_array([$this->redis, 'scan'], $params);
         // test 2
         $this->redis->autoscan('scan', $params, $cbEnd, 50);
     };
     call_user_func_array([$this->redis, 'mset'], $params);
 }
Ejemplo n.º 30
0
 public function submit(\Threaded $thread, $loop)
 {
     parent::submit($thread);
     return Observable::create(function (ObserverInterface $observer) use($thread, $loop) {
         while ($thread->isRunning()) {
             $loop->tick();
             //var_dump($thread->isRunning());
             //usleep(100);
         }
         $observer->onNext(new Event('/thread/ok', $thread));
         $observer->onCompleted();
     });
 }