Interop\Async\Loop::execute PHP Method

execute() public static method

The loop MUST continue to run until it is either stopped explicitly, no referenced watchers exist anymore, or an exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an error handler or exceptions that would be passed to an error handler but none exists to handle them.
See also: Interop\Async\Loop::setFactory()
public static execute ( callable $callback, Driver $driver = null ) : void
$callback callable The callback to execute.
$driver Interop\Async\Loop\Driver The event loop driver. If `null`, a new one is created from the set factory.
return void
    public static function execute(callable $callback, Driver $driver = null)
    {
        $previousDriver = self::$driver;
        self::$driver = $driver ?: self::createDriver();
        self::$level++;
        try {
            self::$driver->defer($callback);
            self::$driver->run();
        } finally {
            self::$driver = $previousDriver;
            self::$level--;
        }
    }

Usage Example

Example #1
0
 /** @test */
 public function executeStackReturnsScopedDriver()
 {
     $driver1 = new DummyDriver();
     $driver2 = new DummyDriver();
     Loop::execute(function () use($driver1, $driver2) {
         $this->assertSame($driver1, Loop::get());
         Loop::execute(function () use($driver2) {
             $this->assertSame($driver2, Loop::get());
         }, $driver2);
         $this->assertSame($driver1, Loop::get());
     }, $driver1);
 }
All Usage Examples Of Interop\Async\Loop::execute