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

45 lines
1.1 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-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;
}
/**
* 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.
*/
2015-08-29 07:41:00 +02:00
function enqueue(TaskInterface $task /* , ...$args */)
{
2015-08-29 07:41:00 +02:00
return new Coroutine(call_user_func_array([pool(), 'enqueue'], func_get_args()));
}
}