0

では、スレッドを作成してメイン プロセスから切り離し、開始するとします。

stringsでは、スレッドが切り離された後、 やints のようなデータのチャンクを、すでに実行中のスレッドに渡すにはどうすればよいでしょうか?

編集 私が基本的にやっていることは、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を使用しています

4

1 に答える 1

0

実際の質問はかなりあいまいです。あなたがしたいことは、IPC(プロセス間通信)の下での私の理解にあり、いくつかの方法で実装できます(私の知る限り、最も一般的な方法は次のとおりです:http://php.net/manual/en/function.stream -socket-pair.php )。ただし、 rabbitmqのようなある種のキューイングおよびポーリング システムを使用してメッセージをやり取りすることをお勧めします。オーバーヘッドが発生しますが、よく知られており、よく使用されるソリューションです。

于 2016-08-26T13:29:30.157 に答える