What is the correct wa开发者_StackOverflowy 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?
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.
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.
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?
精彩评论