Hi guys, I can't figure out how to read the return value of a function started in background. I tried to pass some values by reference or by using an object but with no luck.
How can I read the return value of a function called in background (when it's finished)?
For example this code:
function my_random($obj){
$obj->parameter[0] = 1;
watchdog('random' , $obj->parameter[0]);
}
function get_info($valori)
{
$par = new StdClass();
$par->parameter = array(0);
$handle = background_process_start('my_random', $par);
while(!background_process_is_finished($handle)){
dpm("wait");
}
if(background_process_is_finished($handle)){
dpm($par->parameter[0]);
}
will always print '0', never '1'. Is there a way to do it? I saw that in the end line 574 of the .module is a call_user_func_array($process->callback, $process->args) so passing an object should work.
So is this somehow possible or it's not possible to read the values computed in background inside the main process?
Comments
Comment #2
mcdoolz CreditAttribution: mcdoolz commentedHey there; I checked for the background process by storing the handle as a variable then checking for it when a restart of the process was sought.
When the process is started, it gets the variable (the handle for the process) and then uses background_process_get_process(variable) to get the process. If the process is not empty, then we have a process in progress and shouldn't do anything; else we start a process and set the variable anew.
Insofar as a call back when the process is finished goes, I would suggest having the process itself simply fire a function to set whatever value when finished.
I hope that helps.
Comment #3
surfingaround CreditAttribution: surfingaround commentedThanks for the comment.
But still I don't get how to retry the return value of the callback!
If I have this code:
where does the '1' returned from the foo() function get stored? I need to read that 1! I tried to pass a reference variable or an object, store the value to be returned in there and then when I look back at the object in the calling function nothing changed!
I think the solution to this stays in the way the 'args' are treated inside the background_process, since they are stored inside a db table and then retrieved when necessary. I tried to create another field called "return_value" in the db and store there the result of the call_user_func_array (which is the return value of the foo() function) but still I can't get it working. Any other idea? There must be an easier way to do this right?
Comment #4
mcdoolz CreditAttribution: mcdoolz commentedSpecifically speaking, I do not know.
Have you tried looking at the background_process object? That would make sense on the outset..
I haven't gone that far yet, but you can call the process up by handle using the function I showed above; then just spit it out with print_r or dpm.
Comment #5
surfingaround CreditAttribution: surfingaround commentedWell yeah I tried several times but there is some mess with the db which I cannot understand... even trying to update the db with the newer version of "args" does not help.
EDIT:
I solved by writing in the DB the returned value but it's definitely not a good solution. Still need more ideas.