1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-04 02:27:55 +01:00
parallel/src/Worker/functions.php

65 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace Icicle\Concurrent\Worker;
if (!function_exists(__NAMESPACE__ . '\pool')) {
/**
2015-08-29 03:30:53 +02:00
* Returns the global worker pool for the current context.
*
2015-08-29 07:41:00 +02:00
* @param PoolInterface|null $pool A worker pool instance.
2015-08-28 19:04:04 +02:00
*
2015-08-29 07:41:00 +02:00
* @return PoolInterface The global worker pool instance.
*/
2015-08-29 07:41:00 +02:00
function pool(PoolInterface $pool = null)
{
static $instance;
2015-08-29 07:41:00 +02:00
if (null !== $pool) {
$instance = $pool;
} elseif (null === $instance) {
$instance = new Pool();
}
if (!$instance->isRunning()) {
2015-08-29 03:30:53 +02:00
$instance->start();
}
return $instance;
}
/**
* @coroutine
*
* Enqueues a task to be executed by the worker pool.
*
* @param TaskInterface $task The task to enqueue.
*
* @return \Generator
*
* @resolve mixed The return value of the task.
*/
function enqueue(TaskInterface $task)
{
return pool()->enqueue($task);
}
/**
* @param \Icicle\Concurrent\Worker\WorkerFactoryInterface|null $factory
*
* @return \Icicle\Concurrent\Worker\WorkerInterface
*/
function create(WorkerFactoryInterface $factory = null)
{
static $instance;
if (null !== $factory) {
$instance = $factory;
} elseif (null === $instance) {
$instance = new WorkerFactory();
}
$worker = $instance->create();
$worker->start();
return $worker;
}
}