Time Out in message queues in PHP

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
praveenpvs
Forum Newbie
Posts: 6
Joined: Fri Dec 10, 2010 8:32 am

Time Out in message queues in PHP

Post by praveenpvs »

Hi Everyone,

I am writing PHP script which will communicate with C program through mesage queue.
In the PHP, i am waiting for the message from C program using the
msg_receive
function.
I want to come out of receiving after say 30 secs if i do not get any message.
msg_receive has one option, MSG_IPC_NOWAIT which will come out immediately if no data is available.
Is there way to stop the msg_receive blocking call after 30 secs if no data is available?
I tried my best to search for it but could not succeed .I am new to PHP programming.
Can you any one help me out?
Thanks a lot for your kind help.

regards
Praveen.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Time Out in message queues in PHP

Post by Darhazer »

Use MSG_IPC_NOWAIT and a loop calling msg_receive / sleep
and check for 30 seconds timeout on your own, using time() before entering loop and comparing current time with initial one
praveenpvs
Forum Newbie
Posts: 6
Joined: Fri Dec 10, 2010 8:32 am

Re: Time Out in message queues in PHP

Post by praveenpvs »

Hi Darhazer and all...

Thanks darhazer for your reply....
I was looking for indefinite blocking on msg_receive().
I could figure finally. use msg_receive() as usual i mean with flags parameter to be 0.
Before calling msg_receive(), set the alarm using pcntl_alarm(seconds). pcntl_alarm(secs) sends the Signal SIGALRM after specified secs.
handle the SIGALARM signal in the signal handler. once it gets the signal, msg_receive() comes out of blocking with the error code set to EINTR.
I hope this may be useful to someone.
Thanks for your help.

regards
Praveen
Post Reply