I have a caching system build within php that stores the results of a mysql query in xml. I lock the cache when building by creating a lock file with an exclusive write handle, and then rem开发者_如何学Cove it once the cache file is completed.
However, there are times when the script either times out or halts mid execution, leaving the lock file in place, making any further executions of the script think that the cache is always updating.
I have tried checking for the file being a number of minutes old and attempting to get an exclusive write access to the file to refresh the lock and begin execution, however it appears that the file is still open with the previous handle and I cannot open the new handle to ensure the current process is the only one with access and relock the file.
Is there a way to ensure that if the script is halted mid execution, any open file handles get closed and the files involved are available for future processes to access?
Thanks
When the PHP interpreter exits it calls fclose()
on all open files, thus releasing any locks. Maybe you haven't found the right problem yet.
However, when you need to clean up something before the script terminates - use register_shutdown_function()
. The registered function will be called even when the script terminates with an error.
精彩评论