Jump to content

PHP fread()/fwrite() Synchronous/Asynchronous?


SchweppesAle

Recommended Posts

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

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.

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.

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 :P

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.