如何从操作中在后台正确运行 Symfony 任务?

发布于 2024-10-08 07:54:18 字数 286 浏览 0 评论 0原文

在单独的进程中运行 Symfony 任务的正确方法是什么?我的第一个猜测是使用 fork/exec,但根据 this ,你不能用任何保持打开文件描述符或连接的东西(比如 MySQL)来做到这一点。所以这听起来不像是一个选择。另一种选择是执行 exec('symfony taskname &') ,但这看起来像是一个 hack。这是我能做的最好的事情吗?还有第三条路吗?

What is the correct way to run Symfony tasks in a separate process. My first guess would be to use fork/exec, but according to this, you can't do it with anything that keeps open file descriptors or connections (like MySQL). So that doesn't sound like its an option. Another alternative is to do exec('symfony taskname &'), but that seems like a hack. Is that the best I can do? Is there a third way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉梦枕江山 2024-10-15 07:54:18

通常处理这个问题的方法是使用任务队列。当您想要执行后台进程时,将其添加到某种队列中(您可以使用数据库,或者可以使用实际的队列守护进程,如 beanstalkd)。然后,您将拥有一些守护进程,其工作是将工作从队列中拉出并执行它。

The way this is generally handled is to use a task queue. When you want to do a background process, add it to a queue of some kind (you could use your database, or you could use an actual queue daemon like beanstalkd). You then have some daemonized procces(es) whose job is to pull work out of the queue and perform it.

菩提树下叶撕阳。 2024-10-15 07:54:18

我最终是这样做的:

exec('nohup ' . sfConfig::get('sf_root_dir') . '/symfony TASKNAME >/dev/null &');

您必须重定向 STDOUT,否则它不会在后台运行(尽管如果您想要实际输出,则不必使用 /dev/null )。就我而言,我将所有任务设置为使用 Symfony 的文件记录器,所以这不是问题。

但我仍在寻找更好的解决方案。这看起来像是一个黑客。

Here's how I ended up doing it:

exec('nohup ' . sfConfig::get('sf_root_dir') . '/symfony TASKNAME >/dev/null &');

You have to redirect STDOUT, or else it won't run in the background (though you don't have to use /dev/null if you want the actual output). In my case I set up all my tasks to use Symfony's file logger, so it wasn't an issue.

I'm still looking for a better solution though. This seems like a hack.

城歌 2024-10-15 07:54:18

PHP 不支持多线程。
是的,在我看来,这是 php 的一个大缺陷。
有一种方法可以实现多线程,但不推荐。它很复杂,而且很丑陋,而且它是在提出问题,而不是提出问题。

所以,我认为你能做的最好的事情就是像 exec 这样的事情,或者像调用像 call 这样的网络服务之类的事情?

Php knows no multithreading.
And yes it is a big flaw in php IMO.
There is a way to do multithreading, but it is not recommended. It's complex, and it is ugly and it is asking, no calling out for problems.

So, i think the best you can do is something like exec, or maby somthing like calling a webservice like call?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文