Questions about concurrency and stopping a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xavierds
Forum Newbie
Posts: 1
Joined: Thu Aug 06, 2009 12:26 pm

Questions about concurrency and stopping a function

Post by xavierds »

At first Hi everybody, I'm new here and I hope I could learn some php tricks and help some people too.

My problem I'm sure it's easier but I didn't find a lot of documentation about it.

I'm using a SoapService and I'm making some request at it. This is working great but sometimes the server load it's too high and I cannot wait 10-20 seconds to have an answer, so it's better for me to give back an answer like "Server it's overload try later...".

I read how to do this and the only way I found it's to use concurrency, it's seems normal so I thought about creating a child process who it's calling the SoapService and a father who it's waiting some time and if the time exceed it have to give me back an error.

For the theory I think nothing is wrong, or I'm completly mistaken ?

For the code the way I found to do this in php is this one:

Code: Select all

 
$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {
     // I'm the father
     pcntl_wait($status); 
} else {
      // I'm the crazy son
     $result = __soapCall();
}
 
The problem is in the exit of the children process I saw I can only pass an integer and I need to pass the object who the SoapCall is giving me back, how to do it ?. The other one it's how to make wait only the father 10 seconds for example.

I try this but it didn't seems to work form me:

Code: Select all

 
//Code inside the parent part
$start = microtime(true);
$find = false;
while ( !$find && $start + 10 > microtime(true)){
pcntl_wait($status, WNOHANG);
if(isset($status)
  $find = true;
}
 
if(!find)
  throw new Exception("Server overload, try in few minutes");
 
I hope you can help me to fix this code or just told me some way to make it.

Thanks a lot,

Regards,

Xavier Delcour
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Questions about concurrency and stopping a function

Post by Weirdan »

You can tell soap client to time out using default_socket_timeout ini setting.
Post Reply