6
6
655362015-11-29 15:11:54
PHP
65536, 2015-11-29 15:11:54

How to explain such behavior?

Now I screwed on the old proven blocking of the launch of an already running script and it didn’t work, I found the reason by typing, but now I wonder why this
is the working option

<?php

$lockFile = fopen('running.lock', 'w');

function isRunning($lockFile)
{
    return !flock($lockFile, LOCK_EX | LOCK_NB);
}

if (isRunning($lockFile)) {
    die('running' . PHP_EOL);
} else {
    // ...
    sleep(5);
}

and here is the non-working one
<?php

function isRunning()
{
    $lockFile = fopen('running.lock', 'w');
    
    return !flock($lockFile, LOCK_EX | LOCK_NB);
}

if (isRunning()) {
    die('running' . PHP_EOL);
} else {
    // ...
    sleep(5);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question