Обычно используется с PHP блокировка файла подход. Один создает файл RUN_LOCK
или аналогичный файл и запрашивает file_exists("RUN_LOCK")
. Эта система также используется для защиты потенциальных бесконечных циклов в рекурсивных потоках.
Я решил потребовать файла для выполнения. Другим подходом может быть то, что существование файла вызывает алгоритм блокировки. Это зависит от вашей ситуации. Всегда более безопасная ситуация должна быть легче достичь.
код Wait:
/*prepare the program*/
/* ... */
/*Block until its time to go*/
define("LOCK_FILE", "RUN_UNLOCK"); //I'd define this in some config.php
while(!file_exists(LOCK_FILE)) {
usleep(1); //No sleep will eat lots of CPU
}
/*Execute the main code*/
/* ... */
/*Delete the "run" file, so that no further executions should be allowed*/
usleep(1); //Just for sure - we want other processes to start execution phase too
if(file_exists(LOCK_FILE))
unlink(LOCK_FILE);
Я думаю, было бы неплохо иметь функцию блокировки для этого, как этот:
function wait_for_file($filename, $timeout = -1) {
if($timeout>=0) {
$start = microtime(true)*1000; //Remember the start time
}
while(!file_exists($filename)) { //Check the file existence
if($timeout>=0) { //Only calculate when timeout is set
if((microtime(true)*1000-$start)>$timeout) //Compare current time with start time (current always will be > than start)
return false; //Return failure
}
usleep(1); //Save some CPU
}
return true; //Return success
}
Он реализует тайм-аут. Вы им не нужны, но, может быть, кто-то другой.
Использование:
header("Content-Type: text/plain; charset=utf-8");
ob_implicit_flush(true);while (@ob_end_clean()); //Flush buffers so the output will be live stream
define("LOCK_FILE","RUN_FOREST_RUN"); //Define lock file name again
echo "Starting the blocking algorithm. Waiting for file: ".LOCK_FILE."\n";
if(wait_for_file(LOCK_FILE, 10000)) { //Wait for 10 secconds
echo "File found and deleted!\n";
if(file_exists(LOCK_FILE)) //May have been deleted by other proceses
unlink(LOCK_FILE);
}
else {
echo "Wait failed!\n";
}
Это Выведет:
Starting the blocking algorithm. Waiting for file: RUN_FOREST_RUN
Wait failed!
~or~
Starting the blocking algorithm. Waiting for file: RUN_FOREST_RUN
File found and deleted!
Я хотел бы использовать файл и проверить 'file_exists'. Здесь он действительно разлагает то, что означает «то же время». –
«в то же время» означает параллельно и все начинаются как можно ближе к одному и тому же моменту. Когда вы говорите «check file_exists», вы имеете в виду со спин-блокировкой? Это может сработать. – Rich
Да, что-то с функцией '' usleep' '(http://cz2.php.net/manual/en/function.usleep.php). –