fopen question
Posted: Tue Jul 24, 2012 1:53 pm
Hi!
I'm trying to cache a file which will be accessed thousands of times in the next 20 seconds (online records of a game). I wanted to use semaphores but once I had everything working, the web server had them disabled.
my code now (using fopen) looks like this:
// (1) open cached file
$f=fopen ($fn,"rb");
if ($f!=FALSE) outputCachedFile();
// (2) create new file
$f=fopen ($fn,"xb");
if ($f==FALSE) { file is being created, wait a little bit and then load with outputCachedFile }
// (3) create data, then write it and close $f
When I run this code concurrently, the first thread creates the file at (2) , but the second one calls fopen at (1) and waits for (2) in the other thread to finish.
That's absolutely perfect for me, but I'd like to know if I can expect the same behaviour for every php version (>5) or this is just a coincidence of my server and this PHP version.
Thanks a lot!
Kak
I'm trying to cache a file which will be accessed thousands of times in the next 20 seconds (online records of a game). I wanted to use semaphores but once I had everything working, the web server had them disabled.
my code now (using fopen) looks like this:
// (1) open cached file
$f=fopen ($fn,"rb");
if ($f!=FALSE) outputCachedFile();
// (2) create new file
$f=fopen ($fn,"xb");
if ($f==FALSE) { file is being created, wait a little bit and then load with outputCachedFile }
// (3) create data, then write it and close $f
When I run this code concurrently, the first thread creates the file at (2) , but the second one calls fopen at (1) and waits for (2) in the other thread to finish.
That's absolutely perfect for me, but I'd like to know if I can expect the same behaviour for every php version (>5) or this is just a coincidence of my server and this PHP version.
Thanks a lot!
Kak