Sign up ×

I have a class WorkerPool that has an array declared as

private $live_resources = array();

In the class there is a loop that under a certain condition, adds an item to the array like this

$this->live_resources[$id] = 0;

After this there is a worker started as a new process. Once the worker has completed a task, it calls the method on WorkerPool called live_fixture_processed() by way of a callback. The callback is defined as

$callback = array($this, 'live_fixture_processed');

Then the callback is called in the worker by

call_user_func($callback, $id);

This method changes the value in the array like this

$this->live_resources[$id] = 1;

It then calls another method that uses that array, and the change to the array looks to have been successful.

Meanwhile the class continues on its loop, and after a while it gets to using the $this->live resources array, but the value has reverted to 0!

I have also added a string variable

private $called = '' 

that is also set in the callback, but in the subsequent loop has not been changed either. Is there an issue with the scope of a private variable used in a callback?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Short answer: I can't do this.

Long answer: I was forgetting that the worker is in fact a separate physical process to the workerpool running process. What I am doing may appear to work, but that is because when the child process is forked from the pool process it contains all the code of the original parent process, but it is now just running its own event loop. If I need the child processes to communicate anything more than a simple exit signal back to the parent workerpool process I had to use some form of IPC (Inter Process Communication)

share|improve this answer

Your Answer


