2016-08-21 23:40:48 -05:00
|
|
|
<?php declare(strict_types = 1);
|
2015-08-28 00:18:50 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Worker;
|
2015-08-28 00:18:50 -05:00
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
use Interop\Async\Awaitable;
|
2015-08-29 00:41:00 -05:00
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
/**
|
|
|
|
* Returns the global worker pool for the current context.
|
|
|
|
*
|
2016-08-23 16:47:40 -05:00
|
|
|
* @param \Amp\Parallel\Worker\Pool|null $pool A worker pool instance.
|
2016-08-18 11:04:48 -05:00
|
|
|
*
|
2016-08-23 16:47:40 -05:00
|
|
|
* @return \Amp\Parallel\Worker\Pool The global worker pool instance.
|
2016-08-18 11:04:48 -05:00
|
|
|
*/
|
|
|
|
function pool(Pool $pool = null): Pool {
|
|
|
|
static $instance;
|
2015-08-28 00:18:50 -05:00
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
if (null !== $pool) {
|
|
|
|
$instance = $pool;
|
|
|
|
} elseif (null === $instance) {
|
2016-08-22 18:25:19 -05:00
|
|
|
$instance = new DefaultPool;
|
2015-08-28 00:18:50 -05:00
|
|
|
}
|
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
if (!$instance->isRunning()) {
|
|
|
|
$instance->start();
|
2015-08-28 00:18:50 -05:00
|
|
|
}
|
2015-09-02 08:51:59 -05:00
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueues a task to be executed by the global worker pool.
|
|
|
|
*
|
2016-08-23 16:47:40 -05:00
|
|
|
* @param \Amp\Parallel\Worker\Task $task The task to enqueue.
|
2016-08-18 11:04:48 -05:00
|
|
|
*
|
|
|
|
* @return \Interop\Async\Awaitable<mixed>
|
|
|
|
*/
|
|
|
|
function enqueue(Task $task): Awaitable {
|
|
|
|
return pool()->enqueue($task);
|
|
|
|
}
|
2016-01-11 09:32:06 -06:00
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
/**
|
|
|
|
* Creates a worker using the global worker factory.
|
|
|
|
*
|
2016-08-23 16:47:40 -05:00
|
|
|
* @return \Amp\Parallel\Worker\Worker
|
2016-08-18 11:04:48 -05:00
|
|
|
*/
|
|
|
|
function create(): Worker {
|
|
|
|
$worker = factory()->create();
|
|
|
|
$worker->start();
|
|
|
|
return $worker;
|
|
|
|
}
|
2015-09-02 08:51:59 -05:00
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
/**
|
|
|
|
* Gets or sets the global worker factory.
|
|
|
|
*
|
2016-08-23 16:47:40 -05:00
|
|
|
* @param \Amp\Parallel\Worker\WorkerFactory|null $factory
|
2016-08-18 11:04:48 -05:00
|
|
|
*
|
2016-08-23 16:47:40 -05:00
|
|
|
* @return \Amp\Parallel\Worker\WorkerFactory
|
2016-08-18 11:04:48 -05:00
|
|
|
*/
|
|
|
|
function factory(WorkerFactory $factory = null): WorkerFactory {
|
|
|
|
static $instance;
|
2015-09-02 08:51:59 -05:00
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
if (null !== $factory) {
|
|
|
|
$instance = $factory;
|
|
|
|
} elseif (null === $instance) {
|
2016-08-22 18:25:19 -05:00
|
|
|
$instance = new DefaultWorkerFactory;
|
2016-01-11 09:32:06 -06:00
|
|
|
}
|
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a worker from the global worker pool.
|
|
|
|
*
|
2016-08-23 16:47:40 -05:00
|
|
|
* @return \Amp\Parallel\Worker\Worker
|
2016-08-18 11:04:48 -05:00
|
|
|
*/
|
|
|
|
function get(): Worker {
|
|
|
|
return pool()->get();
|
2015-08-28 00:18:50 -05:00
|
|
|
}
|