mirror of
https://github.com/amphp/parallel.git
synced 2025-02-22 05:42:36 +01:00
87 lines
2.0 KiB
PHP
87 lines
2.0 KiB
PHP
<?php
|
|
namespace Icicle\Concurrent\Worker;
|
|
|
|
if (!function_exists(__NAMESPACE__ . '\pool')) {
|
|
/**
|
|
* Returns the global worker pool for the current context.
|
|
*
|
|
* @param \Icicle\Concurrent\Worker\Pool|null $pool A worker pool instance.
|
|
*
|
|
* @return \Icicle\Concurrent\Worker\Pool The global worker pool instance.
|
|
*/
|
|
function pool(Pool $pool = null): Pool
|
|
{
|
|
static $instance;
|
|
|
|
if (null !== $pool) {
|
|
$instance = $pool;
|
|
} elseif (null === $instance) {
|
|
$instance = new DefaultPool();
|
|
}
|
|
|
|
if (!$instance->isRunning()) {
|
|
$instance->start();
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
* @coroutine
|
|
*
|
|
* Enqueues a task to be executed by the global worker pool.
|
|
*
|
|
* @param \Icicle\Concurrent\Worker\Task $task The task to enqueue.
|
|
*
|
|
* @return \Generator
|
|
*
|
|
* @resolve mixed The return value of the task.
|
|
*/
|
|
function enqueue(Task $task): \Generator
|
|
{
|
|
return pool()->enqueue($task);
|
|
}
|
|
|
|
/**
|
|
* Creates a worker using the global worker factory.
|
|
*
|
|
* @return \Icicle\Concurrent\Worker\Worker
|
|
*/
|
|
function create(): Worker
|
|
{
|
|
$worker = factory()->create();
|
|
$worker->start();
|
|
return $worker;
|
|
}
|
|
|
|
/**
|
|
* Gets or sets the global worker factory.
|
|
*
|
|
* @param \Icicle\Concurrent\Worker\WorkerFactory|null $factory
|
|
*
|
|
* @return \Icicle\Concurrent\Worker\WorkerFactory
|
|
*/
|
|
function factory(WorkerFactory $factory = null): WorkerFactory
|
|
{
|
|
static $instance;
|
|
|
|
if (null !== $factory) {
|
|
$instance = $factory;
|
|
} elseif (null === $instance) {
|
|
$instance = new DefaultWorkerFactory();
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
* Gets a worker from the global worker pool.
|
|
*
|
|
* @return \Icicle\Concurrent\Worker\Worker
|
|
*/
|
|
function get(): Worker
|
|
{
|
|
return pool()->get();
|
|
}
|
|
}
|