Page 1 of 1
PHP service/daemon with asynch calls
Posted: Sun May 28, 2006 8:27 am
by Jagin
Hi
I'd like to have a PHP service/daemon running - to which I can forward asynchronous calls. Basically I need to call to start work on something, but not wait for the work to complete before returning.
What's the best way to approach this?
Thanks
Jagin
Posted: Sun May 28, 2006 9:10 am
by Chris Corbyn
PHP is not right for that project plain and simple

Sorry that's the only response you've got but it's not really made for things like this. Oh they joy I would have if PHP could run asynchrously, my mailer would be firing off commands to twenty servers at the same time

Posted: Sun May 28, 2006 9:41 am
by Ambush Commander
Forking. Not exactly sure how it works, but that's how you'd get asynchronous stuff. Can't fiddle with it on my machine since I'm on Windows and that's pcntl_*
Edit Here's a link:
Process Control Functions. Surprisingly, it does allow daemonizing a process! You would...
Code: Select all
if (pcntl_fork()) {
exit;
}
pcntl_setsid();
if (pcntl_fork()) {
exit;
}
// process is now daemonized
Posted: Sun May 28, 2006 9:43 am
by feyd
You can probably do it with fsockopen() to initiate separate requests behind the scenes too. But that's a hack in my book.
Posted: Sun May 28, 2006 9:52 am
by Chris Corbyn
Oh my bad

I'll have to have a look at those functions, they could come in very handy indeed.
Posted: Sun May 28, 2006 9:54 am
by Ambush Commander
Well, you're right, PHP really isn't too well adapted for command line scripting, and especially for daemons. But it can be done.
Really, I know this stuff because I can't figure out Perl. >.>
Posted: Sun May 28, 2006 9:55 am
by Chris Corbyn
feyd wrote:You can probably do it with fsockopen() to initiate separate requests behind the scenes too. But that's a hack in my book.
Nah I've tried and tried on this one and it's just not happenin' sadly. PHP will just hang while it waits for something to complete.
Thanks
Posted: Sun May 28, 2006 11:08 am
by Jagin
Thanks for all the replies.
Found this interesting write-up on a request for multithreading support in PHP (denied for the 1,000th time):
http://www.zend.com/zend/week/week266.php#Heading5
I think I'll go with Mono to write the daemon.
Jagin
Re: Thanks
Posted: Sun May 28, 2006 11:40 am
by Christopher
For good reason as I understand the issue because it forces a lot of other language decisions that would make PHP more like general purpose languages like Java, C#, etc.
Jagin wrote:I think I'll go with Mono to write the daemon.
That sounds like a good decision -- mainly because you really need a different style of garbage collector for long running processes than PHP provides. PHP is optimized for web request processing.