When trying to implement a locking mechanism to prevent duplicate processing of a longer running script, i experienced something i wasn’t aware of: PHP seems to write to sessions and cache after a request is complete.
A simple test script should show this:
<?php if (apc_fetch('locked')) die ('this script is still running!'); apc_store('locked',true); sleep(10); apc_delete('locked'); echo "Success!";
When i run this script the same time in 2 tabs i would expect, that as long as the script in tab A is loading, i get the die() message in tab B. That’s not the case! Both scripts run, no matter if the first script saved a lock.
The same happens if i use $_SESSION to save some locking flag.
The only explanation i have: the actual cached value is only stored after the script ends.
Does anyone know, how to circumvent this?
My last idea would be to create lock file as file operations (hopefully) don’t get delayed until the script ends.