Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared memory for processes #5666

Open
Domex777 opened this issue Jan 20, 2025 · 1 comment
Open

Shared memory for processes #5666

Domex777 opened this issue Jan 20, 2025 · 1 comment

Comments

@Domex777
Copy link

There are only 3 items in the documentation for shared memory, these are swole tables, locks, and atomics. I have a situation where I need to accept a request and then send this data to a process that makes requests to third-party services. Next, I need to get a response from these third-party services, but in the current reality, this can only be done through swoole tables and a while loop.
The question is, will there be something like this in the future, or is there already a solution now?
Example

Server

private function initData(): void
    {
        $socketPath = config('unix-socket-config.socket_file_path');
        $this->server = new Server($socketPath, 9501, SWOOLE_PROCESS, SWOOLE_UNIX_DGRAM);

        $this->initProcess();

        Co::set(['hook_flags' => SWOOLE_HOOK_ALL]);

        $this->server->set([
            'worker_num' => 1,
            'daemonize' => false,
            'log_file' => config('unix-socket-config.socket_log_file'),
            'enable_coroutine' => true,
            'dispatch_mode' => 1,
        ]);

        $this->server->on('Packet', function (Server $server, string $data, array $address) {

            $socket = $this->process->exportSocket();
            $tracing = bin2hex(random_bytes(10));
            dump("rec_dara: $data");
            dump("sleep_sec: 10-$data");

            $this->data[$tracing] = new Channel(1);

            $socket->send($tracing);

            $response = $this->data[$tracing]->pop();

            var_dump($response);

            $server->send($address['address'], "success");
        });

        $this->server->addProcess($this->process);
    }

    private function initProcess(): void
    {

        $this->process = new Process(
            callback: function (Process $process) {

                dump(ContextManager::get('test'));

                $socket = $process->exportSocket();

                while (true) {

                    $key = $socket->recv();

                    $message = "received-$key";
                    $this->data[$key]->push($message); //but here will be an exception due to key not exist 
                }
            },
            pipe_type: 1,
            enable_coroutine: true
        );
    }

Swoole 6
Gcc version 13.2.1 20240309 (Alpine 13.2.1_git20240309)
PHP 8.4.1 (cli)

@NathanFreeman
Copy link
Member

Inter-process communication would be better achieved using Swoole Table or a third-party solution like Redis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants