So I just want to write this quick and dirty module to a program that takes the current time, echos a string, and then waits x minutes and echos another string. The only thing is, is this little module stops the rest of the program until it's finished. Any way around this?
You can duplicate the current process on which it is running to leave 1 process waiting and the other one do what it has to do.
To fork a PHP process you have to use pcntl_fork
Ofcourse forking would do the trick, but why not use the ticks functionality in PHP?
Consider this example:
declare(ticks=1);
function tick() {
// check if the interval is correct before proceding
$mem = memory_get_usage(true);
echo "Using $mem bytes right now";
}
register_tick_function('tick');
// do all kinds of things in your main program here
while(true) {
usleep(50000);
}
While sleeping it will periodically call the tick function, without having to invoke it manually. The only caveat is that if a particular operation takes a very long time to complete (e.g. a database query) the ticks will not fire until this operation is complete.