PHP - 长时间运行的后台任务

2022-01-08 00:00:00 background php

我目前正在尝试从我可用的内容中生成关于 demenad 的 epub.不幸的是,当 epub 的内容很多时,http 请求需要一段时间(在某些情况下需要 10 分钟)才能完成 - 这并不理想

I am currently trying to generate an epub on demenad from content that is available to me. Unfortunately, when there is alot of content for the epub, it takes a while (10 mins in some cases) for the http request to complete - which is not ideal

我想采用类似于 Safari 的方法- 生成 epub 并在文档可用时向用户发送电子邮件

I want to follow an approach similar to that of Safari - Generate an epub and email the user when the document is available

我的问题是 - 在 PHP 中运行可能需要很长时间才能完成的后台任务/线程的最佳方式是什么

My question is - what is the best way for running a background task/thread in PHP that could take a long time to complete

推荐答案

你要小心长时间运行的 PHP 进程,因为 PHP 的内存效率不是很高(例如 PHP 中只有 100 个整数的数组可以消耗多达 15KB 的内存).这通常适用于 99% 的用例,因为大多数人只是在网站上使用 PHP,并且这些进程运行时间只有几分之一秒,因此为了速度而牺牲了内存.但是,对于长时间运行的流程(尤其是如果您有很多流程),这可能不是您的最佳解决方案.

You want to be careful with long-running PHP processes as, for one PHP is not very memory efficient (as an example an array of just 100 ints in PHP can consume as much as 15KB of memory). This is normally fine for 99% of the use cases since most people are just using PHP for websites and those processes run for fractions of a second so memory is sacrificed for speed. However, for a long-running process (especially if you have a lot of them) this may not be your best solution.

您还需要非常小心地在 PHP 中调用类似 exec/shell_exec 的函数,因为它们在内部实现为流(即它们可能导致父进程阻塞,因为它通常必须等待流返回数据).

You also want to be very careful calling exec/shell_exec like functions in PHP as they are internally implemented as streams (i.e. they can cause blocking in the parent process as it normally has to wait on the stream to return data).

后台任务的一个选项是使用 fork.但是,我强烈建议使用像 gearman 这样的合适的作业管理器(请参阅 php扩展名)或队列,例如 amqp 或 zmq,更干净地处理这些任务.哪个更适合你的用例,我让你决定.

One option to background the task is to use fork. However, I strongly suggest using a proper job manager like gearman (see php extensions also), or queue, like amqp or zmq, to handle these tasks more cleanly. Which one is more suitable for your use case, I'll let you decide.

相关文章