openswoole / ext-openswoole

Programmatic server for PHP with async IO, coroutines and fibers
https://openswoole.com
Apache License 2.0
809 stars 51 forks source link

Http Server EventLoop Has Already Been Created #172

Closed cesurapp closed 2 years ago

cesurapp commented 2 years ago

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.

    \Co\run(function () {
    echo 'Starting server ...'.PHP_EOL;
    
    $server = new \Swoole\Http\Server('127.0.0.1', 9502);
    $server->set([
            \Swoole\Constant::OPTION_ENABLE_COROUTINE => false,
        ]);
    
    Process::signal(SIGTERM, function (int $signal) use ($server) {
        // Disable SIGTERM - SIGINT vs.
    });
    $server->start();
    });
  2. What did you expect to see?

  3. What did you see instead? Warning: Swoole\Server::start(): eventLoop has already been created, unable to start Swoole\Http\Server in /Users/ramazanapaydin/www/ec/test/api/bin/index.php on line 14

  4. What version of OpenSwoole are you using (show your php --ri openswoole)? 4.10.0

  5. What is your machine environment used (show your uname -a & php -v & gcc -v) ? PHP 8.1.3 Macos 11.6.4

doubaokun commented 2 years ago

You can use either co\run to create context or use server callback to create. Remove co\run it should work.

cesurapp commented 2 years ago

I tried and the result is the same.

echo 'Starting server ...'.PHP_EOL;

$server = new \Swoole\Http\Server('127.0.0.1', 9502);
$server->set([
    \Swoole\Constant::OPTION_ENABLE_COROUTINE => false,
]);

\Swoole\Process::signal(SIGTERM, function (int $signal) {
    // Disable SIGTERM - SIGINT vs.
});
$server->start();
Starting server ...
PHP Warning:  Swoole\Server::start(): eventLoop has already been created, unable to start Swoole\Http\Server in /Users/ramazanapaydin/www/ec/test/api/bin/index.php on line 13

Try it yourself. I've been dealing with this for 2 days.

doubaokun commented 2 years ago

I don’t know what you like to archive. Please check server docs at https://openswoole.com/docs/modules/swoole-server-doc Normally, you should not mix server with process.

Rastusik commented 11 months ago

I think he wanted to react to SIGTERM while running the server. Is there any other way to achieve this?

viktike commented 4 months ago

I think he wanted to react to SIGTERM while running the server. Is there any other way to achieve this?

Like this:

    function signal_handler($signal){
        $GLOBALS['server']->shutdown();
    }
    pcntl_signal(SIGINT, "signal_handler");
    pcntl_signal(SIGTERM, "signal_handler");
    Swoole\Timer::tick(200, function(){
        pcntl_signal_dispatch();
    });