Search code examples
phpmultithreadingperformanceprocess

most simple way to start a new process/thread in PHP


Scenario:

  1. Shared hosting, so no ability to install new extensions + no CRON
  2. A submitted request needs to perform some heavy processes.
  3. I want the answer to the client to go as fast as possible, and the heavy lifting to continue immediately, but not stop the client.
  4. can be on a new thread (if it is possible) also no problem with starting a new process.

What is the best way to do this?


Solution

  • On *nix:

    exec('/path/to/executable > /dev/null 2>&1 &');
    

    On Windows:

    $shell = new COM('WScript.Shell'); 
    $proc = $shell->Run('C:\path\to\executable.exe', 0, false);
    

    Both of these will spawn a new process that will run a-synchronously, completely disconnected from the parent. As long as your host allows you to do it.