SchweppesAle Posted October 22, 2009 Share Posted October 22, 2009 Hi, this probably a stupid question. If multiple clients are accessing the same php script which is then reading and writing to the same file, are there any measures in place to ensure that this is done asynchronously or do I need to incorporate this into my own algorithm. Thanks Link to comment https://forums.phpfreaks.com/topic/178619-php-freadfwrite-synchronousasynchronous/ Share on other sites More sharing options...
Mchl Posted October 22, 2009 Share Posted October 22, 2009 Multiple scripts can read simultaneously from one file, but for writing the file is locked. This is handled by file system, not by PHP I suppose. Link to comment https://forums.phpfreaks.com/topic/178619-php-freadfwrite-synchronousasynchronous/#findComment-942105 Share on other sites More sharing options...
SchweppesAle Posted October 22, 2009 Author Share Posted October 22, 2009 Pretty sure the server is running on a linux OS. I've been experimenting with the idea of using AJAX to tally banner impressions since bots are still fairly limited in their ability to execute javascript. Is writing a script which queries the database immediately upon each impression really the only solution? Come to think of it....does a mysql database asynchronously update itself?? I feel like I could be over thinking this. Link to comment https://forums.phpfreaks.com/topic/178619-php-freadfwrite-synchronousasynchronous/#findComment-942112 Share on other sites More sharing options...
Mchl Posted October 22, 2009 Share Posted October 22, 2009 MySQL can deal with multiple writes to single table by locking this table (or just some rows of it), but queries accessing these rows are not rejected, but queued until locks are released. [added] You could possibly emulate this behaviour for flat file, by checking if script managed to open file for writing, and if not trying again after let's say 10ms or so. Just be sure to give up after a while, or you'll just timeout. Link to comment https://forums.phpfreaks.com/topic/178619-php-freadfwrite-synchronousasynchronous/#findComment-942115 Share on other sites More sharing options...
SchweppesAle Posted October 22, 2009 Author Share Posted October 22, 2009 MySQL can deal with multiple writes to single table by locking this table (or just some rows of it), but queries accessing these rows are not rejected, but queued until locks are released. [added] You could possibly emulate this behaviour for flat file, by checking if script managed to open file for writing, and if not trying again after let's say 10ms or so. Just be sure to give up after a while, or you'll just timeout. like this? $fp = fopen('data.txt', 'w'); while(!$fp) { if($fp) { fwrite($fp, 'blah'); } } I think it all comes to whether PHP places a lock on the file once it's been opened. currently I'm just using the following query $bannerid = $_POST['id']; $query = "UPDATE jos_fabanner SET impressions = impressions + 1, dailyimpressions = dailyimpressions + 1 WHERE bannerid = $bannerid"; $result = mysql_query($query); Hopefully this will be able to handle stress testing and provide accurate results Link to comment https://forums.phpfreaks.com/topic/178619-php-freadfwrite-synchronousasynchronous/#findComment-942202 Share on other sites More sharing options...
Mchl Posted October 22, 2009 Share Posted October 22, 2009 More like $connectionCounter = 0; while($connectionCounter < 10 && !$handle = fopen('data.txt', 'w')) { usleep(10000); //wait for 0.01s $connectionCounter++; } if(!$handle) { trigger_error('Couldn't open file for writing',E_USER_WARNING); } Didn't test it. Link to comment https://forums.phpfreaks.com/topic/178619-php-freadfwrite-synchronousasynchronous/#findComment-942296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.