ethan.roday
ethan.roday

Reputation: 2635

PHP queue multiple script calls

I have a PHP script that opens an XML document, gets the DOM and modifies it, and then overwrites the original document.
As I understand it, PHP calls are made asynchronously, so multiple users could be accessing the document at the same time and then saving to it with the second save overwriting the first.
I need this not to be the case. I can't use flock() since that only applies to the current process, so how can I accomplish this?

Upvotes: 1

Views: 424

Answers (2)

Peter
Peter

Reputation: 2556

Actually, flock() doesn't only apply to the current process:

http://php.net/manual/en/function.flock.php

It does use the local filesystem, though, which may become an issue if you're using load-balanced webservers. Also, in order for flock to work you have to make sure that any other processes which may be "competing" for the file are also using flock, otherwise the processes will "step" on each other.

Upvotes: 3

viclim
viclim

Reputation: 959

Lock the file.

Create an empty file (e.g. xml.lock) to indicate that the xml is being opened by other process. Remove the empty file after finish modifying the xml.

Upvotes: -1

Related Questions