2016-08-26 7 views
0

그래서 내가 스레드를 만들고 주 프로세스에서 분리하고 시작한다고 가정 해 봅시다.PHP에서 실행중인 스레드에 데이터를 전달하는 방법은 무엇입니까?

따라서 스레드가 분리 된 후에 이미 실행중인 스레드에 strings 또는 int과 같은 데이터 청크를 전달할 수 있습니까?

편집 내가 기본적으로 WS 프로토콜을 구현하기 위해 노력하고있는 중이 야 : 나는 실행중인 스레드에 데이터를 전달 관련된 다양한 답을 찾을

<?php 
// Pseudo-Code 
class LongRunningThread extends \Thread { 
    private $handshakeReq; 

    public function __construct(Request $handshakeRequest) { 
     $this->handshakeReq = $handshakeRequest; 
    } 

    public function run() { 
     // Do handshake 
     // But do not exit, because after the handshake is done the socket connection needs to be maintained. 
     // Probably some trigger which notifies that a new message is here and the message arrives <automagically> 
     if(trigger) { 
      $message = $message; 
      $this->onNewWsMessage($message); 
     } 
    } 

    public function onNewWsMessage(string $rawMessage) { 
     // Process the message... 
    } 
} 

$stream = stream_socket_server(sprintf("tcp://%s:%d", 
    "localhost", 
    1337 
), $errno, $errmsg); 

// Boiler plate, and connection acceptance (blah blah blah) 
// $client is the the accepted connection 
$message = fread($client, 4096); 

// Cannot pass the $client in here because the instability of resources with threads 
// as passing them here, apparently converts them to <bool> false 
$longRunningThread = new \LongRunningThread($message); 
$longRunningThread->start() && $longRunningThread->join(); 

을하지만 구체적으로 어떤을 찾을 수 없습니다 PHP. 내가 pthreads

답변

0

실제 질문을 사용하고는

매우 모호합니다. 당신이하고 싶은 것은 IPC (프로세스 간 의사 소통)에 대한 나의 이해에서 벗어나 두 가지 방법으로 구현할 수 있습니다. (가장 일반적인 지식은 http://php.net/manual/en/function.stream-socket-pair.php입니다.) 비록 어떤 종류의 대기열을 사용하고 메시지를 전달하기 위해 rabbitmq과 같은 시스템을 폴링 할 수 있다고 제안합니다. 일부 오버 헤드가 있지만 널리 사용되는 잘 알려진 해결책이 될 것입니다.

+0

거기에 코드를 추가했습니다. 당신은 대답에 주어진 링크에 대해 약간의 것들을 설명합니다 =) – Ikari

관련 문제