티스토리 뷰

클래스의 일부 처리를 자식 프로세스에 위임하여 해결 방법을 구현했습니다.이 프로세스는 종료시로드 된 모든 클래스 정의를 Hades로 가져갑니다.

다음과 같이 보입니다.

    foreach (array_chunk($classes, 300) as $classesPortion)
    {
            if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $socketArray) === false)
                        throw new \Exception('Could not create socket');
                        
                                $pid = pcntl_fork();
                                        if ($pid === -1) //Forking failed
                                                {
                                                            throw new \Exception('Could not fork process');
                                                                    }
                                                                            elseif ($pid === 0) //Is child process
                                                                                    {
                                                                                                socket_close($socketArray[1]);
                                                                                                            foreach ($classesPortion as $class)
                                                                                                                        {
                                                                                                                                        $data = ...; //generating needed data
                                                                                                                                                    }
                                                                                                                                                                $dataString = serialize($data);
                                                                                                                                                                            if (!socket_write($socketArray[0], $dataString, strlen($dataString)))
                                                                                                                                                                                        {
                                                                                                                                                                                                        throw new \Exception('Failed to write to socket');
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                socket_close($socketArray[0]);
                                                                                                                                                                                                                                            exit(0);
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                            else //Is parent process
                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                socket_close($socketArray[0]);
                                                                                                                                                                                                                                                                                            pcntl_waitpid($pid, $childProcessStatus);
                                                                                                                                                                                                                                                                                                        if ($childProcessStatus !== 0)
                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                    throw new \Exception('Child process exited abnormally');
                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                            else
                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                        $result = socket_read($socketArray[1], 1000000, PHP_BINARY_READ);
                                                                                                                                                                                                                                                                                                                                                                                                        ... // deal with result
                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                socket_close($socketArray[1]);
                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                            


출처
https://stackoverflow.com/questions/39940206
댓글
공지사항
Total
Today
Yesterday
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30