Saturday, November 27, 2010

php 5.3, ticks, pcntl_signal, pcntl_signal_dispatch

I'm trying to track down a problem with a script that works on php 5.2 but behaves strangely on php 5.3 (there are lots of differences between the environments, and I suspect php version will actually turn out to be completely unrelated). php 5.3 introduced pcntl_signal_dispatch() which processes outstanding signals and I've been investigating if that could somehow explain the behaviour differences I see.

The confusing part is that I've seen people saying that the old way of "declare(ticks=1)" is now deprecated in 5.3, and you must use pcntl_signal_dispatch(). This seemed very silly as you'd have to litter your code with calls to pcntl_signal_dispatch(), as well as have very different code for php 5.2 and 5.3.

If you're confused, like I was, here is what you need to know:
1. declare(ticks=1) still works: no deprecated message (with E_ALL|E_STRICT error reporting);

2. My script, using ticks, behaves identically under 5.2 and 5.3 when I send it a ctrl-C or a kill signal;

3. The docs don't mention it being deprecated; I realized everywhere saying this was user-contributed comments or blogs!

There is a performance aspect with using declare(ticks=1). I believe it is minor, but I think pcntl_signal_dispatch() has been introduced so you can use it instead of ticks if you want to take fine-control over when signals get considered.

5 comments:

Unknown said...

I wonder if you saw this tutorial?

Unknown said...

Hi Keith,
I had a quick skim, and it looks a good complement to the PHP manual, but was there any bit you specifically had in mind (e.g. to do with differences between 5.2 and 5.3)? Did it disagree with the conclusion I came to?

Unknown said...

This section refers you to the process control tutorial which I mentioned. This tutorial has hints that may be useful -- e.g. "you need to configure PHP with the flag --enable-pcntl" -- and examples of preferred usage.

Anonymous said...

ticks were deprecated for about a year before being undeprecated

Unknown said...

Thanks Anon! That would explain the user comments saying it has been deprecated and the docs not mentioning it.