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

44 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace Icicle\Concurrent\Worker;
2015-08-29 03:30:53 +02:00
use Icicle\Coroutine\Coroutine;
if (!function_exists(__NAMESPACE__ . '\pool')) {
/**
2015-08-29 03:30:53 +02:00
* Returns the global worker pool for the current context.
*
2015-08-28 19:04:04 +02:00
* If the pool has not been initialized, a minimum and maximum size can be given to create the pool with.
*
* @param int|null $minSize The minimum number of workers the pool should spawn.
* @param int|null $maxSize The maximum number of workers the pool should spawn.
* @param WorkerFactoryInterface|null $factory A worker factory to be used to create new workers.
*
2015-08-29 03:30:53 +02:00
* @return Pool The global worker pool instance.
*/
2015-08-28 19:04:04 +02:00
function pool($minSize = null, $maxSize = null, WorkerFactoryInterface $factory = null)
{
static $instance;
2015-08-28 19:04:04 +02:00
if (null === $instance) {
2015-08-29 03:30:53 +02:00
$instance = new Pool($minSize, $maxSize, $factory);
$instance->start();
}
return $instance;
}
/**
* Enqueues a task to be executed by the worker pool.
*
* @param TaskInterface $task The task to enqueue.
*
2015-08-29 03:30:53 +02:00
* @return \Icicle\Promise\PromiseInterface
*
* @resolve mixed The return value of the task.
*/
function enqueue(TaskInterface $task)
{
2015-08-29 03:30:53 +02:00
return new Coroutine(pool()->enqueue($task));
}
}