views:

102

answers:

6

Hi everybody,
How could I make sure that the startProcess(); function is being called, but without halting the execution for myFunction(). I'll guess that there's a way to call a function and prevent it from returning it's value to thereby accomplishing this?

Pseudo-code:

function myFunction() {

    startProcess();

    return $something;       
}

function startProcess() {
    sleep(5);
    // Do stuff that user doesn't should have to wait for.
}
+5  A: 

You can't do it. There are some a few functions in PHP that allow async I/O, but nothing like the concurrency you require.

The reason for existing no language support is that PHP is designed to execute short-lived scripts, while the concurrency is managed by the HTTP daemon.

See also:

Artefacto
+1  A: 

As far as I can tell from your question and tags, you want to do some background processing, meaning, essentially, multiple threads.

Unfortunately, PHP doesn't do this. There are some IO functions that are asynchronous, but in general you cannot do concurrent processing in PHP.

Randolpho
+3  A: 

To make a small addition to Artefecto's answer, there are some people who've attempted to recreate a sort of threads situation. You can find some information on it using google, but I doubt it'll be helpful as it's just too experimental and probably pretty unreliable.

Found one link that might be helpful for you.

CharlesLeaf
+1  A: 

What is it you want startProcess() to do? There are many ways to keep the user from having to wait.

Emails are a good example: the thread that runs mail() spins until the message is accepted or rejected; you don't want a user to have to wait for that. So you queue up the task, and then process your queue on cron.

function myFunction() {
    addToQueue();
    return $something;       
}

function addToQueue() {
    // add stuff to the queue of tasks
}

function runQueue() {
    // process the queue of tasks; called by cron.
}
Entendu
+1  A: 

Have you looked at Gearman for farming out this kind of background task?

Mark Baker
+1  A: 

I'm going to take a shot in the dark here, but does this function look like it could be a solution for your overall goal ?

http://php.net/manual/en/function.register-shutdown-function.php

joebert