라이브러리

[PHP] SwooleServer::shutdown - 마스터 서버 프로세스를 종료합니다. 이 함수는 작업자 프로세스에서 호출될 수 있습니다.




Swoole Server Shutdown


Swoole Server는 PHP를 사용하여 개발한 고성능 네트워크 서버입니다. Swoole Server는 여러 가지 이벤트를 처리할 수 있는 이벤트 드리븐 아키텍처를 사용합니다. Swoole Server의 shutdown 이벤트는 서버가 종료될 때 호출되는 이벤트입니다.

Swoole Server::shutdown


Swoole Server의 shutdown 이벤트는 `SwooleServer::shutdown` 메서드를 사용하여 호출할 수 있습니다. 이 메서드는 서버가 종료될 때 호출되는 이벤트를 등록합니다.

# 예제


#hostingforum.kr
php

use SwooleHttpServer;



$server = new Server('127.0.0.1', 9501);



$server->on('start', function ($server) {

    echo "Server is started.
";

});



$server->on('shutdown', function ($server) {

    echo "Server is shutting down.
";

    // shutdown 이벤트에서 수행할 작업을 추가합니다.

    // 예를 들어, 데이터베이스 연결을 끊거나, 파일을 삭제하는 등...

});



$server->on('request', function ($request, $response) {

    $response->write("Hello World");

    $response->end();

});



$server->start();



# shutdown 이벤트에서 수행할 작업


shutdown 이벤트에서 수행할 작업은 서버가 종료될 때 호출되는 이벤트에서 수행할 작업을 등록합니다. 예를 들어, 데이터베이스 연결을 끊거나, 파일을 삭제하는 등...

#hostingforum.kr
php

$server->on('shutdown', function ($server) {

    // 데이터베이스 연결을 끊습니다.

    $db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');

    $db->close();



    // 파일을 삭제합니다.

    unlink('/path/to/file.txt');

});



# shutdown 이벤트에서 수행할 작업을 취소하는 방법


shutdown 이벤트에서 수행할 작업을 취소하는 방법은 `SwooleServer::shutdown` 메서드를 사용하여 shutdown 이벤트를 취소합니다.

#hostingforum.kr
php

$server->on('shutdown', function ($server) {

    // shutdown 이벤트를 취소합니다.

    $server->shutdown(false);

});



# shutdown 이벤트에서 수행할 작업을 취소하는 방법 (2)


shutdown 이벤트에서 수행할 작업을 취소하는 방법은 `SwooleServer::shutdown` 메서드를 사용하여 shutdown 이벤트를 취소하고, shutdown 이벤트에서 수행할 작업을 등록합니다.

#hostingforum.kr
php

$server->on('shutdown', function ($server) {

    // shutdown 이벤트를 취소합니다.

    $server->shutdown(false);



    // shutdown 이벤트에서 수행할 작업을 등록합니다.

    $server->on('shutdown', function ($server) {

        echo "Server is shutting down.
";

        // shutdown 이벤트에서 수행할 작업을 추가합니다.

        // 예를 들어, 데이터베이스 연결을 끊거나, 파일을 삭제하는 등...

    });

});



결론


Swoole Server의 shutdown 이벤트는 서버가 종료될 때 호출되는 이벤트입니다. shutdown 이벤트에서 수행할 작업을 등록하여 서버가 종료될 때 수행할 작업을 지정할 수 있습니다. shutdown 이벤트에서 수행할 작업을 취소하는 방법은 `SwooleServer::shutdown` 메서드를 사용하여 shutdown 이벤트를 취소합니다. shutdown 이벤트에서 수행할 작업을 취소하는 방법 (2)은 shutdown 이벤트를 취소하고, shutdown 이벤트에서 수행할 작업을 등록하는 방법입니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 95 페이지

검색

게시물 검색