PHP 单独的模块 - 最佳实践
PHP separate module - Bst Practices
我对 php 很感兴趣,我更擅长 c++。
这是我的问题。
我需要实现这样的序列:
第一个进程执行一些计算,将其存储在数据库(或文件)中
第二个进程回答用户请求并传递最后的计算结果(由第一个进程存储)。
执行此操作的最佳做法是什么?
我假设是这样的:
if (!calculatorProcess.running())
{
calculatorProcess.run([]{
// do something like this
while (1) {
performCalculations();
storeSomewhere();
sleep(1);
}
}
}
getLastCalculationResults();
sendResponce();
提前致谢
就其价值而言,您实际上不应该以这种方式设计 PHP 应用程序。它在理论上意味着完全同步,因为它应该处理本身完全同步的 HTTP 请求。
鉴于您的示例,如果任务 1 是一个 long-running 进程,您最终将需要任务 2 休眠一段时间,或者轮询控制任务 1 的任何内容以查看它是否完成.这样做,如果您不响应,您 运行 将面临 HTTP 请求超时的风险。如果您必须这样做,最好在一个 PHP 流程中完成整个业务。
一个更好的方法(但不是唯一的方法)来处理这个问题,假设任务 1 是一个足够长的 运行ning 过程,用户会注意到,可能是做这样的事情:
//PHP - longrunningprocess.php
$jobId = generateJobId();
//Actually launch the long-running process. There are alternatives to exec, such Icicle:
//https://github.com/icicleio/icicle
exec('/path/to/long_running_script.php arg1 arg2 arg3');
echo '{ "jobId":'.$jobId.'}';
//end
//PHP - longrunningprocessresult.php
$jobId $_GET['jobId'];
var result = new LongRunningProcessResult();
var jobStatus = getJobStatus();
if(jobStatus.complete != true)
{
result.complete = true;
result.property1 = jobStatus.property1;
//...
}
else
{
result.complete = false;
}
echo json_encode(result);
然后在客户端,像这样:
function handleJobSuccess(results)
{
//Do whatever you do with the results
}
function checkForCompletion(jobId)
{
setTimeout(function() {
$.ajax("longrunningprocessresult.php?jobId=" + jobId, {success: function(args){
if(args.success)
{
handleJobSuccess(args);
}
else
{
checkForCompletion(jobId);
}
}
});
}, 5000);
}
function beginLongRunningProcess()
{
showWorkingThrobber();
$.ajax('longrunningprocess.php', { success: function(args){
var jobId = args.jobId;
checkForCompletion(jobId);
}});
}
重要的一点是,您不希望向服务器发送请求的时间很长 运行ning,因为它只会在 运行 时占用浏览器。您需要实现一个 UI 至少向用户暗示进程正在运行,然后异步处理结果。
一般来说,您应该将PHP视为一种脚本语言。与 Linux 上的 bash 脚本或 Windows 上的 Power Shell 脚本相同。如果您必须定期处理数据,您可以安排 process.php 脚本定期执行以处理数据或由发送数据的脚本执行。当用户请求数据时,query.php 应该 return 从数据库中处理的数据。因此,您不需要两个线程或进程,而是两个单独的文件,它们将由您的服务器 运行(当用户访问时)或由 PHP 在命令行上(当 运行ning 作为计划任务)。希望我答对了你的问题,这回答了你的问题。
我对 php 很感兴趣,我更擅长 c++。 这是我的问题。
我需要实现这样的序列:
第一个进程执行一些计算,将其存储在数据库(或文件)中
第二个进程回答用户请求并传递最后的计算结果(由第一个进程存储)。
执行此操作的最佳做法是什么? 我假设是这样的:
if (!calculatorProcess.running())
{
calculatorProcess.run([]{
// do something like this
while (1) {
performCalculations();
storeSomewhere();
sleep(1);
}
}
}
getLastCalculationResults();
sendResponce();
提前致谢
就其价值而言,您实际上不应该以这种方式设计 PHP 应用程序。它在理论上意味着完全同步,因为它应该处理本身完全同步的 HTTP 请求。
鉴于您的示例,如果任务 1 是一个 long-running 进程,您最终将需要任务 2 休眠一段时间,或者轮询控制任务 1 的任何内容以查看它是否完成.这样做,如果您不响应,您 运行 将面临 HTTP 请求超时的风险。如果您必须这样做,最好在一个 PHP 流程中完成整个业务。
一个更好的方法(但不是唯一的方法)来处理这个问题,假设任务 1 是一个足够长的 运行ning 过程,用户会注意到,可能是做这样的事情:
//PHP - longrunningprocess.php
$jobId = generateJobId();
//Actually launch the long-running process. There are alternatives to exec, such Icicle:
//https://github.com/icicleio/icicle
exec('/path/to/long_running_script.php arg1 arg2 arg3');
echo '{ "jobId":'.$jobId.'}';
//end
//PHP - longrunningprocessresult.php
$jobId $_GET['jobId'];
var result = new LongRunningProcessResult();
var jobStatus = getJobStatus();
if(jobStatus.complete != true)
{
result.complete = true;
result.property1 = jobStatus.property1;
//...
}
else
{
result.complete = false;
}
echo json_encode(result);
然后在客户端,像这样:
function handleJobSuccess(results)
{
//Do whatever you do with the results
}
function checkForCompletion(jobId)
{
setTimeout(function() {
$.ajax("longrunningprocessresult.php?jobId=" + jobId, {success: function(args){
if(args.success)
{
handleJobSuccess(args);
}
else
{
checkForCompletion(jobId);
}
}
});
}, 5000);
}
function beginLongRunningProcess()
{
showWorkingThrobber();
$.ajax('longrunningprocess.php', { success: function(args){
var jobId = args.jobId;
checkForCompletion(jobId);
}});
}
重要的一点是,您不希望向服务器发送请求的时间很长 运行ning,因为它只会在 运行 时占用浏览器。您需要实现一个 UI 至少向用户暗示进程正在运行,然后异步处理结果。
一般来说,您应该将PHP视为一种脚本语言。与 Linux 上的 bash 脚本或 Windows 上的 Power Shell 脚本相同。如果您必须定期处理数据,您可以安排 process.php 脚本定期执行以处理数据或由发送数据的脚本执行。当用户请求数据时,query.php 应该 return 从数据库中处理的数据。因此,您不需要两个线程或进程,而是两个单独的文件,它们将由您的服务器 运行(当用户访问时)或由 PHP 在命令行上(当 运行ning 作为计划任务)。希望我答对了你的问题,这回答了你的问题。