라이브러리

[PHP] SwooleChannel::push - Swoole 채널에 데이터를 쓰고 푸시합니다.




SwooleChannel::push 개요


SwooleChannel은 Swoole의 내장 채널 기능입니다. 채널은 프로세스 간의 통신을 위한 메시징 시스템입니다. SwooleChannel::push는 채널에 메시지를 푸시하는 메서드입니다.

사용법


SwooleChannel::push를 사용하려면 먼저 SwooleChannel을 생성해야 합니다. 채널을 생성한 후, 메시지를 푸시할 수 있습니다.

#hostingforum.kr
php

use SwooleChannel;



// 채널 생성

$channel = new Channel(10);



// 채널에 메시지 푸시

$channel->push('Hello, World!');



예제 1: 채널에 메시지 푸시하기


#hostingforum.kr
php

use SwooleChannel;



// 채널 생성

$channel = new Channel(10);



// 채널에 메시지 푸시

$channel->push('Hello, World!');



// 메시지 가져오기

$message = $channel->pop();

echo $message . "
"; // Hello, World!



예제 2: 채널에 메시지 푸시하고 가져오기


#hostingforum.kr
php

use SwooleChannel;



// 채널 생성

$channel = new Channel(10);



// 채널에 메시지 푸시

$channel->push('Hello, World!');



// 메시지 가져오기

$message = $channel->pop();

echo $message . "
"; // Hello, World!



// 채널에 메시지 푸시

$channel->push('안녕하세요, 세계!');

// 메시지 가져오기

$message = $channel->pop();

echo $message . "
"; // 안녕하세요, 세계!



예제 3: 채널에 메시지 푸시하고 여러 개 가져오기


#hostingforum.kr
php

use SwooleChannel;



// 채널 생성

$channel = new Channel(10);



// 채널에 메시지 푸시

$channel->push('Hello, World!');

$channel->push('안녕하세요, 세계!');

$channel->push('Bonjour, monde!');



// 메시지 여러 개 가져오기

while ($message = $channel->pop()) {

    echo $message . "
";

}

// Hello, World!

// 안녕하세요, 세계!

//Bonjour, monde!



예제 4: 채널에 메시지 푸시하고 블록킹으로 가져오기


#hostingforum.kr
php

use SwooleChannel;



// 채널 생성

$channel = new Channel(10);



// 채널에 메시지 푸시

$channel->push('Hello, World!');



// 메시지 블록킹으로 가져오기

$message = $channel->pop($timeout = 1);

if ($message) {

    echo $message . "
"; // Hello, World!

} else {

    echo "채널에 메시지가 없습니다.
";

}



예제 5: 채널에 메시지 푸시하고 여러 개 블록킹으로 가져오기


#hostingforum.kr
php

use SwooleChannel;



// 채널 생성

$channel = new Channel(10);



// 채널에 메시지 푸시

$channel->push('Hello, World!');

$channel->push('안녕하세요, 세계!');

$channel->push('Bonjour, monde!');



// 메시지 여러 개 블록킹으로 가져오기

while ($message = $channel->pop($timeout = 1)) {

    echo $message . "
";

}

// Hello, World!

// 안녕하세요, 세계!

//Bonjour, monde!



참고


* SwooleChannel::push는 채널에 메시지를 푸시하는 메서드입니다.
* SwooleChannel::pop은 채널에서 메시지를 가져오는 메서드입니다.
* 채널은 프로세스 간의 통신을 위한 메시징 시스템입니다.
* 채널을 생성한 후, 메시지를 푸시하고 가져올 수 있습니다.
* 메시지를 푸시할 때, 채널에 공간이 없으면 예외가 발생합니다.
* 메시지를 가져올 때, 채널에 메시지가 없으면 예외가 발생합니다.
* 채널에 메시지를 푸시하고 가져올 때, 블록킹으로 가져올 수 있습니다.
* 채널에 메시지를 푸시하고 여러 개 가져올 때, 블록킹으로 가져올 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 174 페이지

검색

게시물 검색