How to avoid php file running when it is being processed?

Asked

Viewed 41 times

1

How to prevent a php file when it is running from being executed by another user?

an example:

User X opens the exe.php file the processing time of it will be in 25 seconds at this time the user Y decides to open the exe.php file but it cannot be executed because the user X is still processing the file.

There is a solution to this?

  • There are even a number of different ways to do this, like the answer from fellow @epx (who already took my +1) but the impression is that you have one XY problem, that is, asking the way you think is the solution, instead of exposing the real problem.

1 answer

3

You can try to create a temporary file, if it is Linux it could be something like /tmp/exe.php.lock, and delete when processing ends. But there is a great risk that the file will not be removed if PHP fails halfway.

Another output is to create a socket on a well-defined, fixed port. If bind() fails it is because another instance already created the socket on that port and has not yet left. You can refer to the example in http://php.net/manual/en/sockets.examples.php to see how to create the socket; just go to bind(), and don’t forget the socket_close() at the end.

and close the socket when the processing is over. The chance of the socket being open if PHP fails halfway is less, or perhaps non-existent.

  • Your answer is better than the original closing post (including I mentioned in the original "orphan" lock problem, which you wisely solved with the bind()). Too bad Sopt’s people don’t merge postage.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.