对于耗时的任务(电子邮件发送,图像处理......你明白了),我想运行异步PHP任务。
它是quite easy on Linux,但我正在寻找一种适用于Windows的方法。
我希望它变得简单,应该如此。没有artillery,没有SQL排队,没有一次又一次installing stuff ...我只想运行一个该死的异步任务。
所以我尝试了Symfony Process Component。 问题是,同步运行任务工作正常,但在异步运行时,它会沿主脚本退出。
有没有办法解决这个问题?
composer require symfony/process
的index.php
<?php
require './bootstrap.php';
$logFile = './log.txt';
file_put_contents($logFile, '');
append($logFile, 'script (A) : '.timestamp());
$process = new Process('php subscript.php');
$process->start(); // async, subscript exits prematurely…
//$process->run(); // sync, works fine
append($logFile, 'script (B) : '.timestamp());
subscript.php
<?php
require './bootstrap.php';
$logFile = './log.txt';
//ignore_user_abort(true); // doesn't solve issue…
append($logFile, 'subscript (A) : '.timestamp());
sleep(2);
append($logFile, 'subscript (B) : '.timestamp());
bootstrap.php中
<?php
require './vendor/autoload.php';
class_alias('Symfony\Component\Process\Process', 'Process');
function append($file, $content) {
file_put_contents($file, $content."\n", FILE_APPEND);
}
function timestamp() {
list($usec, $sec) = explode(' ', microtime());
return date('H:i:s', $sec) . ' ' . sprintf('%03d', floor($usec * 1000));
}
结果
script (A) : 02:36:10 491
script (B) : 02:36:10 511
subscript (A) : 02:36:10 581
// subscript (B) is missing
答案 0 :(得分:2)
异步处理完成后,主脚本必须等待。试试这段代码:
$process = new Process('php subscript.php');
$process->start();
do {
$process->checkTimeout();
} while ($process->isRunning() && (sleep(1) !== false));
if (!$process->isSuccessful()) {
throw new \Exception($process->getErrorOutput());
}
答案 1 :(得分:0)
不是最好的解决方案,但是:
$process = new Process('nohup php subscript.php &');
$process->start();
答案 2 :(得分:0)
如果php supports fpm for Windows,您可以收听<!-- language: lang-js -->
{
"id": "tblCustomer",
"name": "tblCustomer",
"columns": [
{
"id" : "thCustIndex",
"name" : "thCustIndex",
"title" : "Sr. #",
"classes" : "",
"type" : "index",
"sortable" : "true",
"editable" : "false"
},
{
"id" : "thCustId",
"name" : "thCustId",
"title" : "Id",
"classes" : "align-center",
"type" : "text",
"sortable" : "true",
"editable" : "false"
},
{
"id" : "thCustName",
"name" : "thCustName",
"title" : "Name",
"classes" : "",
"type" : "text",
"sortable" : "true",
"editable" : "false"
},
{
"id" : "thCustDob",
"name" : "thCustDob",
"title" : "Date Of Birth",
"classes" : "",
"type" : "date",
"sortable" : "true",
"editable" : "false"
},
{
"id" : "thCustMobile1",
"name" : "thCustMobile1",
"title" : "Mobile 1",
"classes" : "",
"type" : "inMobile",
"sortable" : "true",
"editable" : "false"
},
{
"id" : "thCustEmail",
"name" : "thCustEmail",
"title" : "E-mail Id",
"classes" : "align-center",
"type" : "email",
"sortable" : "true",
"editable" : "false"
},
{
"id" : "thCustAddress",
"name" : "thCustAddress",
"title" : "Address",
"classes" : "",
"type" : "text",
"sortable" : "true",
"editable" : "false"
}
]
事件,以便在发送回复后提供所有昂贵的任务。
服务:
kernel.terminate
监听器:
app.some_listener:
class: SomeBundle\EventListener\SomeListener
tags:
- { name: kernel.event_listener, event: kernel.terminate, method: onKernelTerminate }