How to “fork” a video conversion process into background, in php?

对着背影说爱祢 提交于 2019-12-01 10:55:38

The php manual page for exec() says:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

So, yes, your exec call will do the trick.

Jed Smith

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.

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.

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.

See my answer to this question as well, which is also relevant.

It will do the trick but it seems like an unmanageable idea. If you're just going to launch and never look back, things might go wrong really fast.

How about just scheduling a script that runs every few minutes and polls anything that is still in queue. Do you have access to cron?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!