Is there a way to execute a PHP script if there are no new requests for some time?

Is there any way in PHP to take some action (e.g. in mysql insert) if there are no new queries for 1 second?

What I'm trying to achieve is to determine the beginning and end of a sequence of images sent from an IP camera. The camera sends a series of images to the detected motion and stops sending when the motion stops. I know that the camera takes 5 images per second (every 200 ms). When there are more than 1 second of new images, I want to mark the last image as the end of the sequence, insert the record in mysql, put the img in the appropriate folder (where all the other img from the same sequence are already recorded) and specify the application to make the MJPEG clip of the images in this folder.

Now I can determine the first image in the sequence using Alternative money in PHP to save the original time from the previous request, but the problem is because the next sequence of images can happen in a few hours, and I can not instruct PHP to complete the sequence if for some There are no NO requests for time only when the first request for a new sequence arrives.

I really need help with this. My PHP sucks almost like my English ... :)

Pseudocode for my problem:

<?php if(isset($headers["Content-Disposition"])) { $frame_time = microtime(true); if(preg_match('/.*filename=[\'\"]([^\'\"]+)/', $headers["Content-Disposition"], $matches)) { $filename = $matches[1]; } else if(preg_match("/.*filename=([^ ]+)/", $headers["Content-Disposition"], $matches)) { $filename = $matches[1]; } } preg_match("/(anpr[1-9])/", $filename, $anprs); $anpr = $anprs[1]; $apc_key = $anpr."_last_time"; //there are several cameras so I have to distinguish those $last = apc_fetch($apc_key); if (($frame_time - $last)>1) { $stream = "START"; //New sequence starts } else { $stream = "-->"; //Streaming }; $file = fopen('php://input', 'r'); $temp = fopen("test/".$filename, 'w'); $imageSize = stream_copy_to_stream($file, $temp); //save image file on the file system fclose($temp); fclose($file); apc_store($apc_key, $frame_time); //replace cashed time with this frame time in APC // here goes mysql stuff... /* Now... if there is no new requests for 1 second $stream = "END"; calling app with exec() or similar to grab all images in the particular folder and make MJPEG... if new request arrives cancel timer or whatever and execute script again. */ ?> 
+7
php image ip-camera
source share
3 answers

Was it possible to complete each request 1.5 seconds before exiting, and as a last step to check whether the timestamp of the sequence was updated? If so, go out and do nothing. If not, save the sequence in mysql. (This will require mutexes, as each HTTP request will check and try to keep the sequence, but only one should be allowed.)

This approach would combine the subfile / script into php code (one code base, easier to maintain), but it can use the memory usage in cylinders (each request will remain in memory for 1.5 seconds, which is a very long time for a busy server).

Another approach is to make the sub file / script in the loopback request on the localhost HTTP server, supposedly much less memory. Each frame could cancel the request to complete the sequence (similarly again using mutexes).

Or maybe create a separate service call that checks and stores all sequences, and also performs a cron job every few seconds. Or let each frame check it, if the second request can detect that the service is already running, it can exit. (APC cache sharing status)

Edit: I think I just suggested what is said above.

+1
source share

What if you just hold the script for 1 second after it saves the frame to check for additional frames? I think you can close the connection before 1 second elapses, but tomlgold2003 and arr1 will answer you: http://php.net/manual/en/features.connection-handling.php#93441

I think this will work for you:

 <?php ob_end_clean(); header("Connection: close\r\n"); header("Content-Encoding: none\r\n"); ignore_user_abort(true); // optional ob_start(); // Do your stuff to store the frame $size = ob_get_length(); header("Content-Length: $size"); ob_end_flush(); // Strange behaviour, will not work flush(); // Unless both are called ! ob_end_clean(); // The connection should now be closed sleep(1); // Check to see if more frames have been added. ?> 

If a high load is expected on your server, this may not be the answer for you, since when you receive 5 frames per second, 5 scripts will be checked to see if they sent the last frame.

0
source share

Save every request from the camera with all its data and timestamp in a file (in php-serialization format). In cronjob mode (every 10 seconds or so), a script that reads this file and finds requests that have more than one second after them, before the next request. Save the data from such queries and delete all other queries.

-one
source share

All Articles