2 namespace GuzzleHttp\Promise;
5 * A task queue that executes tasks in a FIFO order.
7 * This task queue class is used to settle promises asynchronously and
8 * maintains a constant stack size. You can use the task queue asynchronously
9 * by calling the `run()` function of the global task queue in an event loop.
11 * GuzzleHttp\Promise\queue()->run();
13 class TaskQueue implements TaskQueueInterface
15 private $enableShutdown = true;
18 public function __construct($withShutdown = true)
21 register_shutdown_function(function () {
22 if ($this->enableShutdown) {
23 // Only run the tasks if an E_ERROR didn't occur.
24 $err = error_get_last();
25 if (!$err || ($err['type'] ^ E_ERROR)) {
33 public function isEmpty()
38 public function add(callable $task)
40 $this->queue[] = $task;
45 /** @var callable $task */
46 while ($task = array_shift($this->queue)) {
52 * The task queue will be run and exhausted by default when the process
53 * exits IFF the exit is not the result of a PHP E_ERROR error.
55 * You can disable running the automatic shutdown of the queue by calling
56 * this function. If you disable the task queue shutdown process, then you
57 * MUST either run the task queue (as a result of running your event loop
58 * or manually using the run() method) or wait on each outstanding promise.
60 * Note: This shutdown will occur before any destructors are triggered.
62 public function disableShutdown()
64 $this->enableShutdown = false;