Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forking in PHP on Windows

Tags:

php

fork

windows

We are running PHP on a Windows server (a source of many problems indeed, but migrating is not an option currently). There are a few points where a user-initiated action will need to kick off a few things that take a while and about which the user doesn't need to know if they succeed or fail, such as sending off an email or making sure some third-party accounts are updated. If I could just fork with pcntl_fork(), this would be very simple, but the PCNTL functions are not available in Windows.

It seems the closest I can get is to do something of this nature:

exec( 'php-cgi.exe somescript.php' );

However, this would be far more complicated. The actions I need to kick off rely on a lot of context that already will exist in the running process; to use the above example, I'd need to figure out the essential data and supply it to the new script in some way. If I could fork, it'd just be a matter of letting the parent process return early, leaving the child to work on a few more things.

I've found a few people talking about their own work in getting various PCNTL functions compiled on Windows, but none seemed to have anything available (broken links, etc).

Despite this question having practically the same name as mine, it seems the problem was more execution timeout than needing to fork. So, is my best option to just refactor a bit to deal with calling php-cgi, or are there other options?

Edit: It seems exec() won't work for this, at least not without me figuring some other aspect of it, as it waits until the call returns. I figured I could use START, sort of like exec( 'start php-cgi.exe somescript.php' );, but it still waits until the other script finishes.

like image 451
Doug Kavendek Avatar asked Mar 16 '10 20:03

Doug Kavendek


People also ask

How do I fork a process in PHP?

An example of doing this is as follows: $pid = pcntl_fork(); if($pid) { // this is the parent process // wait until the child has finished processing then end the script pcntl_waitpid($pid, $status, WUNTRACED); exit; } // the child process runs its stuff here and then ends ...

What is Pcntl_fork?

The pcntl_fork() function creates a child process that differs from the parent process only in its PID and PPID. Please see your system's fork(2) man page for specific details as to how fork works on your system.


1 Answers

how about installing psexec and use the -d (don't wait) option

exec('psexec -d php-cgi.exe somescript.php');
like image 88
jah Avatar answered Sep 28 '22 07:09

jah