如何“分叉"将视频转换为背景的过程,在php中? [英] How to "fork" a video conversion process into background, in php?

查看:102
本文介绍了如何“分叉"将视频转换为背景的过程,在php中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理Flash上​​载脚本,该脚本将视频文件上载到dir.简单的.上传完成后,它将为该文件创建一个mysql记录,然后移至队列中的下一个文件.

I have a batch flash upload script, that uploads video files to a dir. Simple. After the upload completes, it creates a mysql record for that file, and moves on to the next file in the queue.

在执行此操作之前,我希望它调用一个后台进程,该进程会将上载的avi avi文件转换为与ipod兼容的mp4文件,并生成一些预览图.可以想象,这需要一些时间……我可以简单地将转换代码放入文件上传器中……但是它会在每个文件上挂起10到20分钟,这是一个很长时间(甚至您的管理员专用功能).

Just before it does that, I want it invoke a background process that will convert the uploaded avi avi file to an ipod compatible mp4 file, as well as generate some preview thumbs. As you can imagine, it takes some time...... I can simply put the conversion code in the file uploader... but it would hang for every file for a good 10-20 minutes, which is a nono (even thou its an admin-only function).

所以我希望它在后台进行转换过程,并在转换文件时移至下一个上载.

So I want it to fork the conversion process in the background, and move onto the next upload, while it converts the file.

这样的事情会做吗,还是我实际上必须使用php fork函数?

Would something like this do the job, or will I actually have to use the php fork functions?

exec("/usr/bin/php ./convert.php?id=123 > /dev/null 2>&1 &");

推荐答案

在体系结构上实现此目标的最佳方法是工作队列,您的PHP前端将后台驻留程序提供给后端文件进行转换.将PHP与您的工作脱钩,您的UI将始终保持响应状态.

The best way to implement this architecturally is a work queue, with your PHP front end feeding the daemon in the backend files to convert. Decouple PHP from your work, and your UI will always remain responsive.

我已经很长时间没有写PHP了,但是据我了解,任何启动的进程都属于最大超时规则,并在Web服务器下运行.您不希望这样.您当然不希望Web请求能够启动其他流程.

I haven't written PHP in a long time, but it's my understanding that any process started falls under the maximum timeout rule, and is run under the Web server. You don't want that to be the case; you certainly don't want a Web request capable of starting additional processes.

编写一个简单的守护程序,该守护程序在Web服务器外部运行,并监视文件夹中是否有上传的文件.您的Web前端将它们转储到那里,并且守护程序会为每次转换生成一个线程,直到您拥有多少个内核.从结构上来说,这是更明智的选择.

Write a simple daemon that runs outside the Web server and watches a folder for uploaded files. Your Web front end dumps them there, and your daemon spawns off a thread for each conversion up to how many cores you have. Architecturally, this is the wiser choice.

请参阅我对 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆