在收到程序输出时将其打印到网页

发布于 2024-10-18 11:11:40 字数 299 浏览 4 评论 0原文

老实说,我不知道以前是否有人问过这个问题,或者类似的问题。这样的事情很难搜索。

那么,就这样吧。我希望我的网页运行托管在服务器上的程序(PHP 是正确的选择,是吗?),然后使用该程序的输出打印到网页。但是,该程序不会立即打印所有输出,因此,当我之前尝试过此操作时,它会等到程序成功退出后再打印输出,而不是在收到时打印它。 (这是有道理的)

当返回每个新结果时,如何将输出打印到网页?也就是说,在显示网页之后,当程序输出文本行时,将条目添加到 div 中。

阿贾克斯? JavaScript?我只是在寻找解决此问题的正确方法,而不是代码。

I honestly have no idea if this question has been asked before, or something similar like it. It's hard to search on such a thing.

So, here goes. I would like my webpage to run a program (PHP would be the correct choice here, yes?) hosted on the server, and then use the output of that program to print to a webpage. However, the program doesn't print its output all at once, thus, when I've tried this before, it waits until the program exits successfully before printing output, rather than printing it as it is received. (Which makes sense)

How would one go about printing output to the webpage as each new result is returned? That is, after displaying the webpage, have entries be added to a div as the program outputs lines of text.

Ajax? Javascript? I'm just looking for what the be the correct way to go about this, not code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎无痕… 2024-10-25 11:11:40

就个人而言,我会使用三个文件来完成此操作:

  1. 在服务器上运行的脚本 (worker.php)
  2. 客户端调用的 php 文件 (index.php
  3. )第二个php文件处理来自index.php的ajax请求(ajax.php

我的工作流程如下:

  1. 用户调用index.php
  2. index.php 对 worker.php 进行一次调用,后者开始将输出生成到文本文件 (output.txt)
  3. index.php 中,不断 ping < strong>ajax.php 查看是否有任何新内容已添加到 output.txt 中,并随着更多输出可用而不断更新
  4. worker.php 完成时,它在文本文件中输出 EOF 信号,并在 X 秒后将其删除。 (X 是比对 ajax.php 文件执行 ping 操作之间的延迟更大的某个值。)

Personally, I would do this with three files:

  1. The script being run on the server (worker.php)
  2. The php file being called by the client (index.php)
  3. A second php file to process ajax requests from index.php (ajax.php)

My workflow would be as follows:

  1. User calls index.php
  2. index.php makes a single call to worker.php, who starts producing output into a textfile (output.txt)
  3. index.php continually pings ajax.php seeing whether anything new has been added to output.txt, and continually updates as more output is available
  4. When worker.php is done, it outputs an EOF signal in the textfile, and deletes it X seconds later. (X is some value larger than the delay between pings to the ajax.php file.)
爱要勇敢去追 2024-10-25 11:11:40

基本思想是打开一个到子进程的管道并从中读取,就像它是一个文件一样。
特别是对于 PHP,请查看 proc_open

The basic idea is you open a pipe to the child process and read from it as if it were a file.
For PHP in particular, take a look at proc_open.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文