-1

I want to prevent a PHP script from running at the same time. Which I achieved by checking if a 'lock.txt' file exists. This however takes multiple seconds, even though it should be very fast.

<?php
    if (!file_exists('lock.txt')) {
        $lock = fopen('lock.txt','w');

        ////////////////////////////////////////////
        ......script that takes 30-160 seconds......
        ////////////////////////////////////////////

        $jsonArray = array(utf8_encode('script')=>utf8_encode('finished'));
        fclose($lock);
        unlink('lock.txt');
    } else {
        $jsonArray = array(utf8_encode('script')=>utf8_encode('locked'));
    }
    echo json_encode($jsonArray);
?>

Why does it take that long? It should just fail the initial if statement and quickly echo json_encode.

am I doing something wrong?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
displaynn
  • 23
  • 6
  • what's the file's size? – Funk Forty Niner Mar 17 '16 at 14:55
  • 7 kilobytes (670 kilobytes if it includes classes even if the if-statement doesn't pass). It takes long because it requests data from an API (which is kinda slow), not because the script is massive. – displaynn Mar 17 '16 at 14:59
  • if you mean the 'lock.txt' file: it is 0 kilobytes. – displaynn Mar 17 '16 at 15:03
  • Is this via a web interface or command-line? Also you should look into http://php.net/manual/en/function.flock.php – apokryfos Mar 17 '16 at 15:03
  • One more thing. There's always a chance the file gets created between the `if (!file_exists('lock.txt')) {` and the `fopen("lock.txt")` so you shouldn't really rely on this as a mutex mechanism – apokryfos Mar 17 '16 at 15:05
  • @apokryfos web-interface... and: Is there an alternative? – displaynn Mar 17 '16 at 15:08
  • Script executes for me in `0.00099992752075195 seconds`. Even if I put some complex mathematical processes where your code would be, which is irrelevant anyway because it won't execute. – WheatBeak Mar 17 '16 at 15:09
  • @displaynn Add `$time = microtime(true)` at the top of the script and then `echo microtime(true) - $time` at the end to check how long the PHP script actually takes. – apokryfos Mar 17 '16 at 15:11
  • How many files are in the same directory? – WheatBeak Mar 17 '16 at 15:13
  • The script takes: '0.65914744933446' minutes. It takes '1.8994013468424E-6'??? minutes when locked. Also, while adding this i made a small error (forgot an `;`), and this made the script execute immediately (With error though). – displaynn Mar 17 '16 at 15:18
  • @wheatbeak only the maint script, lock.txt file, and another directory with 'NuSOAP' – displaynn Mar 17 '16 at 15:19
  • Can't see any reason it would be taking that long unless your server is powered by a hamster in a wheel. What if you reverse the if statement and check that the file exists first and then immediately exit the script if it does. – WheatBeak Mar 17 '16 at 15:22
  • @apokryfos Second attempt took the script '1.7325083414714E-6' minutes. I have no idea why it returns the value like that. It takes about half the time of the script when it is 'unlocked'. – displaynn Mar 17 '16 at 15:23
  • 1.7325083414714E-6 = 0.0000017325083414714 seconds. Basically 0. Your problem is *not* in the in PHP but elsewhere (web-server/browser). – apokryfos Mar 17 '16 at 15:25
  • that value is seconds not minutes, it's returning it in scientific notation – WheatBeak Mar 17 '16 at 15:26
  • @WheatBeak I reversed the statement, and added `exit;` directly after it, still takes roughly 15-20 seconds. I am stumped. – displaynn Mar 17 '16 at 15:26
  • In that case it's definitely the hamster ; - ), It's either your machine or internet connection that have the slow hamster though not the server, as @apokryfos said. your script is executing fast. – WheatBeak Mar 17 '16 at 15:27
  • I hate hamster powered servers. Hair everywhere. – apokryfos Mar 17 '16 at 15:27
  • @apokryfos Yes, this seems to be the case. Just tried running the script in two different browsers (Chrome + Firefox). Which made it execute immediately. – displaynn Mar 17 '16 at 15:29
  • 1
    @displaynn please tell me you weren't using IE. – apokryfos Mar 17 '16 at 15:30
  • @apokryfos and /@WheatBeak, thanks for helping me :) Nope, was using two tabs in Chrome. – displaynn Mar 17 '16 at 15:31

1 Answers1

0

Problem was client-sided, I used two tabs in the Chrome browser.

Thanks to @apokryfos and @WheatBeak for helping me reach this conclusion.

displaynn
  • 23
  • 6