PHP - 长时间运行的后台任务 [英] PHP - Long Running Background Task

查看:341
本文介绍了PHP - 长时间运行的后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为在对可用我的内容demenad的ePub。
不幸的是,当存在很多的对于epub的内容,还需要一段时间(10分钟在某些情况下)为http请求来完成 - 这是不理想的

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 int数组可以消耗高达内存15KB)。这是,因为大多数人都只是使用PHP的网站使用的情况下99%的正常罚款,这些进程运行第二所以内存牺牲速度为代价的分数。然而,对于一个长期运行的进程(特别是如果你有很多的人),这可能不是你的最佳解决方案。

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).

一个选择背景的任务是使用。然而,我强烈建议使用适当的作业管理器如 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.

这篇关于PHP - 长时间运行的后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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