Jump to content

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


SchweppesAle

Recommended Posts

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
Share on other sites

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
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.