[nycphp-talk] WAMP (minus the M) forking question - also open to other ideas
lists at zaunere.com
Thu Apr 30 07:04:46 EDT 2009
> Hi experienced and talented community of which I have been trolling for
> a few years!
> I have a question for all of you and I hope this makes sense.
> First, the background. I am coding a PHP Web application that has to
> call a external program, however, because only one instance of the
> program can be ran at a time I need to implement a queue. In addition,
> it is important the users need to not wait (keep the browser open) for
> their job to process. The script will send them an e-mail with a link
> after their job completes. This means I need something to persistently
> run whenever there is something in the queue. In summary: front end
> script calls secondary child which runs until queue is empty by calling
> external program.
> My problem is, I cannot figure out how to properly fork under Windows,
> allowing the parent script to run to completion, and not hang the
> browser waiting for the child to complete. I am at the point where I
> can spawn the child and it will run even when the parent browser is
> closed but will not release the parent script until it completes. I
> have tried exec(), system(), shell_exec(), popen(), and a COM call
> (which I might have done wrong). Each of those called php -f
> I thought about a few other alternatives:
> 1. Use a scheduled task to check for a queue at a set interval. This is
> the least desired solution as it could cause unwanted delay.
> 2. Write a PHP daemon/Win32 service to monitor a queue of which could
> be queried and started if needed by parent script and it would run
> until the queue is empty at which time it would shutdown.
> 3. Write a Perl script to be called by the parent which would fork and
> call the secondary PHP script, subsequently terminating and releasing
> the parent PHP script.
> In summary:
> - WAMP environment, cannot use Linux as external program is Windows
> only (BUMMER!)
> - Need a queue to handle calls to an external program, currently stored
> in a flat file
> - Parent script needs to fork to a child script which can run
> independent of the parent until queue is empty
> - Parent script needs to be able to complete prior to child completing
> - Because of time required to run a job and because of the potential
> for many users, users need to be able to submit'n'run.
> Do any of you PHP gods (and goddesses!) have any thoughts on how to
> fork under Windows, how to handle this situation, or alternatives?
> Should I be using some other language? Perl, AJAX, other?
> Google (or my search string) was less than helpful.
Hmm, this is a good question. I know this can happen on Linux/Apache as
well. Basically there's always a problem forking from apache because of
various file descriptors/etc. which are held onto within the forked PHP
process. If you do decide to fork, read:
And specifically the code around "Need to fork so apache doesn't kill us".
Effectively, you need to close down and reopen all filedes that are
inherited into the PHP process, otherwise you get the hang problem you
describe. Search around also for forking from an apache module as there are
other resources on this.
The other option would be to simply execute a batch file which queues and
returns immediately. Another independent process could be the queue runner.
This is more of an "async" way doing this type of thing, and actually may be
preferred for scalability and flexibility reasons.
More information about the talk