mirror of
https://github.com/danog/parallel.git
synced 2024-11-30 04:39:01 +01:00
86 lines
1.6 KiB
PHP
86 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Amp\Parallel\Worker;
|
|
|
|
use Amp\Loop;
|
|
|
|
const LOOP_POOL_IDENTIFIER = Pool::class;
|
|
const LOOP_FACTORY_IDENTIFIER = WorkerFactory::class;
|
|
|
|
/**
|
|
* Gets or sets the global worker pool.
|
|
*
|
|
* @param \Amp\Parallel\Worker\Pool|null $pool A worker pool instance.
|
|
*
|
|
* @return \Amp\Parallel\Worker\Pool The global worker pool instance.
|
|
*/
|
|
function pool(Pool $pool = null): Pool
|
|
{
|
|
if ($pool === null) {
|
|
$pool = Loop::getState(LOOP_POOL_IDENTIFIER);
|
|
if ($pool) {
|
|
return $pool;
|
|
}
|
|
|
|
$pool = new DefaultPool;
|
|
}
|
|
|
|
Loop::setState(LOOP_POOL_IDENTIFIER, $pool);
|
|
|
|
return $pool;
|
|
}
|
|
|
|
/**
|
|
* Enqueues a task to be executed by the global worker pool.
|
|
*
|
|
* @param Task $task The task to enqueue.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
function enqueue(Task $task)
|
|
{
|
|
return pool()->enqueue($task);
|
|
}
|
|
|
|
/**
|
|
* Gets a worker from the global worker pool.
|
|
*
|
|
* @return Worker
|
|
*/
|
|
function get(): Worker
|
|
{
|
|
return pool()->get();
|
|
}
|
|
|
|
/**
|
|
* Creates a worker using the global worker factory. The worker is automatically started.
|
|
*
|
|
* @return Worker
|
|
*/
|
|
function create(): Worker
|
|
{
|
|
return factory()->create();
|
|
}
|
|
|
|
/**
|
|
* Gets or sets the global worker factory.
|
|
*
|
|
* @param \Amp\Parallel\Worker\WorkerFactory|null $factory
|
|
*
|
|
* @return \Amp\Parallel\Worker\WorkerFactory
|
|
*/
|
|
function factory(WorkerFactory $factory = null): WorkerFactory
|
|
{
|
|
if ($factory === null) {
|
|
$factory = Loop::getState(LOOP_FACTORY_IDENTIFIER);
|
|
if ($factory) {
|
|
return $factory;
|
|
}
|
|
|
|
$factory = new DefaultWorkerFactory;
|
|
}
|
|
Loop::setState(LOOP_FACTORY_IDENTIFIER, $factory);
|
|
|
|
return $factory;
|
|
}
|