将 PHP 调用的 .exe 程序(作为单独进程运行,而不是在 PHP 中运行)的输出发送回 iPhone 客户端的可能方法
我的 iPhone 客户端应用程序将数据上传到在 PHP 上运行的服务器。有一段代码可以在 PHP 的服务器端调用 .exe 程序。 .exe 程序将获取上传的数据并在自己的进程上运行。这意味着 PHP 执行将结束,而无需等待 .exe 程序完成。在.exe程序处理完上传的数据并产生输出后,我希望将此输出发送回iPhone。
通常,如果我们调用 .exe 程序在 php 中运行而不将其设为单独的进程,则必须等待该程序完成,然后才能将输出发送回 iPhone 客户端。
通过将 .exe 程序作为单独的进程运行,不可能通过调用 .exe 程序的 PHP 发回数据。问题是,如果我们让 .exe 程序运行在单独的进程而不是 PHP 脚本上,有哪些可能的方法可以将输出发送回 iPhone 客户端?
My iPhone client app uploads a data to the server, which runs on PHP. There is a code to invoke a .exe program on the server side on PHP. The .exe program will take the uploaded data and run on a process on its own. That means the PHP execution will end without waiting for the .exe program to finish. After the .exe program finished processing the uploaded data and have an output, I want this output to be sent back to the iPhone.
Normally, if we call the .exe program to be run inside the php without making it a seperate process, we have to wait for the program to finish and we can send the output back to the iPhone client.
By running the .exe program as a seperate process, it is impossible to send the data back via PHP that invokes the .exe program. The question is, if we have the .exe program running on a seperate process rather than on the PHP script, what are the possible methods to send the output back to the iPhone client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是您概述的一个需求问题。让我解释一下几个想法。
首先,如果您终止初始上传请求,检查其是否完成的唯一合理方法是每隔几秒从 iPhone 进行轮询。每 5 秒向“get-update.php”发送一次请求,查看是否有数据。
通过使用 $_SESSION,您应该能够存储一个令牌,该令牌将在数据处理完成时识别数据。
关于实际过程,您可以通过多种方式来完成。一种是进行相当标准的双分叉,将子进程与父进程分离,这样子进程将在父进程退出后继续运行。
另一种(推荐)是编写一个后端服务器进程,该进程将监视数据库中的请求、获取请求、处理请求并更新数据库。因此,当初始上传脚本实际上传数据时,让PHP将其放入数据库中,将记录ID存储在$_SESSION中,然后返回给用户。
后端进程将注意到有一条记录需要处理,读取数据,调用可执行文件,并用结果更新数据库。
get-update.php 脚本将读取 $_SESSION 来获取记录 id,并检查数据库是否已处理数据(或状态是什么)。
如果您没有能力运行后台进程,并且您仅限于使用 PHP,您可以执行双分叉魔法,并分叉另一个 PHP 进程来执行数据库读取/exe/数据库更新。
有问题请随意评论。
您需要 (a) 一种将数据传递给程序的好方法,以及 (b) 一种取回数据的好方法。
That's a need problem you've outlined. Let me explain a couple of ideas.
First of all, if you terminate the initial upload request, the only resonable way to check for it being done is to poll every few seconds from the iPhone. Send a request to "get-update.php" every 5 seconds to see if you have data.
By using $_SESSION, you should be able to store a token that will identify the data when it has finished processing.
Regarding the actually process, you may be able to accomplish that in a number of ways. One is to do a fairly standard double-fork, detaching the child process from the parent, so it will continue after the parent exits.
Another (recommended) would be to author a backend server process that would watch your database for requests, fetch them, process them, and update the database. So when the inital upload script actually uploads the data, have PHP put it in the database, store the record ID in $_SESSION, and return to the user.
The back end process will notice that there is a record to process, read the data, call the executable, and update the database with the result.
The get-update.php script will read $_SESSION for the record id, and check the database if the data has been processed (or what the status is).
If you do not have the ability to run a background process, and you are constrained to using PHP, you could do the double-fork magic, and fork of another PHP process to do the database read / exe / database update.
Feel free to comment with questions.
You need (a) a good way to pass the data to the program, and (b) a good way to get the data back.
我想说,对于经常从文本文件中轮询数据的 AJAX 片段来说,这是一个完美的案例.exe 写入其状态。
您调用的上传脚本可能会向上传客户端返回某种唯一标识符。使用该标识符,客户端将轮询 exe 的状态(例如“输出文件 xyz 是否已经存在?”),直到获得积极的反馈。
I would say this is a perfect case for an AJAX snippet frequently polling data from, say, a text file the .exe writes its status in.
The upload script you call could return a unique identifier of some sort to the uploading client. Using that identifier, the client would poll the exe's status (e.g. "does the output file xyz already exist?") until it gets positive feedback.
一旦您断开与 iPhone 的连接,您将很难重新连接。它可能不在覆盖范围内,它可能已更改 IP 地址,……
您最好的选择是让 iPhone 重新连接回服务器并轮询其信息。
You're going to have a hard time reconnecting with the iPhone once you've severed the connection. It may be out of coverage, it may have changed IP address, ......
Your best bet is to have the iPhone reconnect back to the server and poll for it's information.
您可以通过使用 Apple 的推送通知服务来做到这一点,但这可能有点矫枉过正,除非您认为数据处理将花费很长时间,和/或您想在处理完成后更新应用程序图标,即使应用程序没有运行。
您是否希望用户只是耐心等待结果,还是他们会先发送数据,然后再回来查看?如果只需要几秒钟,您可以让 iPhone 应用程序在等待一会儿(同时显示进度指示器)后轮询结果。
You could do this by using Apple's Push Notification service, but that's probably overkill, unless you think the data processing is going to take a long time, and/or you want to update the app icon when the processing is done, even if the app isn't running.
Do you expect the user to just be patiently waiting for the result, or are they going to fire off the data, and check back later? If it's only going to take a couple of seconds, you could just have the iPhone app poll for the result after waiting a little while (while displaying a progress indicator).