*/ function enqueue(Task $task): Promise { return pool()->enqueue($task); } /** * Enqueues a callable to be executed by the global worker pool. * * @param callable $callable Callable needs to be serializable. * @param mixed ...$args Arguments have to be serializable. * * @return Promise */ function enqueueCallable(callable $callable, ...$args) { return enqueue(new CallableTask($callable, $args)); } /** * Gets a worker from the global worker pool. * * @return \Amp\Parallel\Worker\Worker */ function worker(): Worker { return pool()->getWorker(); } /** * Creates a worker using the global worker factory. * * @return \Amp\Parallel\Worker\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; }