Ernest Faizullin
Ernest Faizullin

Reputation: 147

Run php script exactly every second?

I need to run php script every second regardless of how long it will run. Tried to do it that way:

#!/usr/bin/python

import threading, time, os

def f():
    os.system('php /var/www/test/artisan time:server > /dev/null &')
    threading.Timer(1, f).start()

f()

but php script is executed with unequal intervals. A little more or a little less than one second. Prompt how correctly to make the intervals of exactly one second? Not necessarily with python.

Upvotes: 0

Views: 374

Answers (2)

A.A
A.A

Reputation: 2713

<?php
set_time_limit(60);
$start = time();

for ($i = 0; $i <= 59; ++$i) {
    // Do whatever you want here
    time_sleep_until($start + $i + 1);
}
?>

Upvotes: 3

Devon Bessemer
Devon Bessemer

Reputation: 35337

The script finishing execution in exactly one second intervals, as in 1000000μs, is going to be nearly impossible to achieve just because of how processing works.

This would require:

  1. A process that spawns exactly every 1000000μs
  2. A CPU always available to accept the runnable process (no other load)
  3. A process that always takes the same amount of time to execute

You may be able to acheive one or two of these, but all three aren't realistic. Number 3 is going to be the main issue.

Upvotes: 1

Related Questions