0

Does PHP provide an option to keep a file open instead of re-opening whenever the script is started?

My script is called around 500 times per second and can be compared to a counter script, which reads 100 lines from the file status.json, does some ++ or -- or resets some values in the file and writes the result back to the same file.

With 500 read/writes per second, the hdd has no issue yet and is not blocking other parallel scripts that r/w the same file. However, this will happen as soon as the number of calls per second increase significantly.

That's why I'd like to keep this file open for r/w by any PHP instances without locks or delay by reopening or closing again and again within each script.

Any tip for this? Has this been solved well with sqlite? If yes, I can switch to sqlite.

3
  • Use a DB that's the way to go, unless you want to save the file in the ram and write on it. Commented May 1, 2013 at 11:38
  • See answers on stackoverflow.com/questions/126917/php-object-caching re memcached and APC Commented May 1, 2013 at 11:42
  • 1
    At 500 r/w per second I would think about using a ram disk or sqlite.org/inmemorydb.html you can then sync it back to a backup every x seconds / min / hr depending on the criticalness of the data. If you use PDO to connect to an sqlite db then you may find transactions useful. Commented May 1, 2013 at 11:43

1 Answer 1

0

Does PHP provide an option to keep a file open instead of re-opening whenever the script is started?

no.

Has this been solved well with sqlite?

sqlite being the very same file that have to be opened closed as well

You may try some intelligent database that does it's operations mainly in RAM but also dumps it's content to disc periodically for safety. Mongo is an example

Sign up to request clarification or add additional context in comments.

1 Comment

i went back one step, split the data into separate that are merged periodically with a little delay, once per XY seconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.