3

On a new PHP session on a linux web server, I want to start a process (specifically ghostscript) running in the background in console input mode and then repeatedly write new data to its input. This data write needs to occur every time new user data arrives via ajax. The user data is interpreted by the PHP script into postscript commands for ghostscript and then needs to be sent to gs. The gs output will be to a named file. The reason for doing this is that I need speed and want to avoid the overhead of starting gs on each new input from the user. (Once gs is running and waiting for input it takes about 40ms to process my data, but restarting gs and processing the data is taking around 250ms.) Although I can get gs to run in the background and accept input from the shell, I am having difficulty getting it to do this from a PHP script. I've been through all the variations of exec, shell_exec, popen, system, coproc, etc. that I can think of and I seem to be missing something fundamental. Thanks for any insights.

4

1 回答 1

1

如果您在服务器上有适当的访问权限,可以启动一个套接字并使 ghostscript 接受来自该套接字的输入。

例如

在 tty 上:
$ socket -sl 11555

在另一个 tty 上:
$ nc 0 11555 | ghostscript

现在,无论您向套接字写入什么内容,gs 都会将其作为输入接收。当您的 AJAX 请求到达服务器时,您可以进行所需的任何处理,然后将生成的 postscript 命令从 PHP 写入套接字。

于 2012-12-17T13:14:03.243 回答