我需要在用户操作时执行目录副本,但目录非常大,所以我希望能够执行这样的操作,而无需用户知道副本完成所需的时间.
任何建议将不胜感激.
我正在尝试创建一个PHP脚本,我已经完成了脚本,但是它需要10分钟才能完成它的设计过程.这不是问题,但我认为我必须一直保持页面加载这很烦人.我可以拥有它以便我启动该过程然后在10分钟后返回并查看它生成的日志文件吗?
我正在使用popen和fgets来异步读取tcpdump的输出.
下面的代码应该在命令行中运行,而不是使用apache并在浏览器中查看.
$handle = popen('tcpdump -nnX', 'r');
while (true) {
$output = fgets($handle);
print $output . "\n";
}
Run Code Online (Sandbox Code Playgroud)
当我尝试通过websockets输出此信息时出现问题.
Websockets还使用无限循环(用于管理其套接字,刻度和消息).
它看起来像:
while (true) {
@socket_select($read,$write,$except,1);
foreach ($read as $socket) {
if ($socket == $this->master) {
$client = socket_accept($socket);
...
Run Code Online (Sandbox Code Playgroud)
我使用$ websocket-> sendToAll($ message);通过websocket发送数据.
我不能一个接一个地放入while循环,因为它只会运行我先放入的循环,while (true) { A() }; while (true) { B() };B()永远不会被调用
我无法合并while循环,因为websockets减慢了popen的读取速度,反之亦然.while (true) { A(); B(); }如果B需要很长时间才能完成,A将会很慢.
在这种情况下我该怎么办?我对线程,分叉脚本之间的通信或其他任何东西的想法持开放态度.
用户提交表单后,我想渲染一个视图文件,然后我想启动一个后台任务来处理五个MS Excel文件(每个文件最多可能有2000行),但是这样就让用户不要不必等待该过程完成才能查看该页面.任务完成后,我将通过电子邮件通知用户.
我正在使用Symfony Framework 3.我在下面包含了我的代码.它没有做我想要实现的目标.提交表单后,页面仅在整个后台任务完成时加载.
我不确定但是经过google搜索后,我认为kernel.terminate事件在这里很有用.但我似乎无法理解如何使用它.
你能告诉我如何解决这个问题吗?
这是我的代码:
我创建了一个控制台命令:
class GreetCommand extends ContainerAwareCommand {
protected function configure()
{
$this->setName('hello:world');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// My code to execute
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我有
$process = new Process('ls -lsa');
$process->disableOutput();
$that = $this;
$process->start(function ($type, $buffer) use ($that) {
$command = new GreetCommand();
$command->setContainer($this->container);
$input = new ArrayInput(array());
$output = new NullOutput;
$resultCode = $command->run($input, $output);
});
return $this->render('success.html.php', array('msg' => 'Registraion Successful'));
Run Code Online (Sandbox Code Playgroud)
我已经使用PHP的连接处理功能解决了这个问题.
感谢这篇文章
我想运行一个非常慢/复杂的程序,至少需要10分钟才能完成.我想通过shell_exec或类似的东西来调用它,但我不希望php脚本等待这个程序的完成.
是我可以做的事情?如果是这样我怎么能这样做?
我ffmpeg在PHP文件中进行了视频转换,它可以正常工作.问题是这需要一分钟才能完成.我认为它可能很容易但我只能在后台工作时只使用一个命令(例如没有mp4box的单通道转换)
exec("nohup " . $ffmpegPath . " -i " . $srcFile . " -f mp4 -vcodec libx264 -crf 27 -s " . $srcWidth . "x" . $srcHeight . " -an -r 20 " . $destFile . ".mp4 > /dev/null 2>&1 &");
Run Code Online (Sandbox Code Playgroud)
问题是我需要使用三个不同的命令来进行正确的转换.到目前为止,我的命令在PHP文件中看起来像这样,但它有效,但是有很大的延迟:
exec($ffmpegPath . " -y -i " . $srcFile . " -f mp4 -pass 1 -passlogfile " . $video_pass_log . " -vcodec libx264 -vpre ipod640 -b:v 2M -bt 4M -an " . $destFile . ".mp4");
exec($ffmpegPath . …Run Code Online (Sandbox Code Playgroud) 我是Symfony2的新手,在尝试运行这样的异步命令时遇到了阻塞:
class MyCommand extends ContainerAwareCommand{
protected function configure()
{
$this
->setName('my:command')
->setDescription('My command')
->addArgument(
'country',
InputArgument::REQUIRED,
'Which country?'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$country = $input->getArgument('country');
// Obtain the doctrine manager
$dm = $this->getContainer()->get('doctrine_mongodb.odm.document_manager');
$users = $dm->getRepository('MyBundle:User')
->findBy( array('country'=>$country));
}}
Run Code Online (Sandbox Code Playgroud)
当我从命令行调用它时,这非常有效:
php app/console my:command uk
Run Code Online (Sandbox Code Playgroud)
但是当我称之为Symfony2进程时,它不起作用:
$process = new Process("php ../app/console my:command $country");
$process->start();
Run Code Online (Sandbox Code Playgroud)
我收到一个数据库错误:" [MongoWriteConcernException] 127.0.0.1:27017:not master "
我认为这意味着该过程没有得到我的数据库配置...
我只想运行异步进程,还有其他方法吗?
也许一种方法来调用不需要答案的应用程序命令继续?
也许我需要使用注射?
PS:我目前的命令只是一个测试,最后应该是一个"昂贵"的操作...
我想从Symfony2中的Controller内异步调用命令.
到目前为止我找到了以下解决方案:
$cmd = $this->get('kernel')->getRootDir().'/console '.(new MLCJobWorkerCommand)->getName().' '.$job->getId().' 2>&1 > /dev/null';
$process = new Process($cmd);
$process->start();
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这一目标?
编辑:
我需要Process在后台运行,Controller需要在启动前者后立即返回.我试过了:
$cmd = $this->get('kernel')->getRootDir().'/console '
.(new MLCJobWorkerCommand)->getName()
.' '.$job->getId().' 2>&1 > /dev/null & echo \$!';
$process = new Process($cmd);
$process->mustRun();
$params["processid"] = $process->getOutput();
Run Code Online (Sandbox Code Playgroud)
但是,在Process完成之前,Controller不会返回响应.
我正在尝试编写一个脚本,该脚本将在服务器上创建一个文件,然后用于header()将用户重定向到该文件.然后,大约10秒后我想删除该文件.我试过这个:
header('Location: '.$url);
flush();
sleep(10);
unlink($url);
Run Code Online (Sandbox Code Playgroud)
但是浏览器只是等待脚本完成然后被重定向,但是那个文件被删除了.有没有办法告诉浏览器"文件结束",然后继续计算?或者可能让PHP启动另一个脚本,但不等待该脚本完成?