PHP service/daemon with asynch calls

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Jagin
Forum Newbie
Posts: 2
Joined: Sun May 28, 2006 8:21 am

PHP service/daemon with asynch calls

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
Last edited by Ambush Commander on Sun May 28, 2006 9:46 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Oh my bad :oops: I'll have to have a look at those functions, they could come in very handy indeed.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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. >.>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Jagin
Forum Newbie
Posts: 2
Joined: Sun May 28, 2006 8:21 am

Thanks

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Thanks

Post by Christopher »

Jagin wrote: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
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.
(#10850)
Post Reply