Real-time interaction-how to provide real-time feedback during PHP Script Execution

Source: Internet
Author: User
Tags apc jquery file upload
There are two pages, one for uploading an EXCEL file, and the other for accepting and processing the EXCEL file. The following is the content displayed during the processing of this page, but each time the program is fully running, it will be displayed. Sometimes it takes too long to receive feedback. the user will close the page and I want to add a real-time prompt. Please kindly advise... there are two pages, one for uploading an EXCEL file, and the other for accepting and processing the EXCEL file. The following is the content displayed during the processing of this page, but each time the program is fully running, it will be displayed. Sometimes it takes too long to receive feedback. the user will close the page and I want to add a real-time prompt. Please give me some advice

I have thought about the asynchronous method, but the second file is in the Ifram framework, and the asynchronous retrieval also needs to wait for the value, and the final display is still the same.

// Read and insert 117 pieces of data and generate a report at 11:04:17. 11:04:31 reading and inserting the database succeeded. 11:04:31 completed adding a worksheet to the workbook. At 11:04:32, the worksheet content is written. At 11:04:32, the worksheet is cloned. At 11:04:32, the clone worksheet [unemployment] was added. At 11:04:33, the worksheet is cloned. At 11:04:33, the clone worksheet [medical care] is added. At 11:04:33, the worksheet is cloned. At 11:04:33, the clone worksheet [Work injury] is added. At 11:04:34, the worksheet is cloned. At 11:04:34, the clone worksheet [fertility] is added. At 11:04:34, the worksheet is cloned. At 11:04:34, the clone worksheet "Major Diseases" is added. At 11:04:35, the worksheet is cloned. At 11:04:35, the clone worksheet [supplemental care] is added. At 11:04:35, set the basic information of the file. 11:04:35. The query result set database is successful. 11:04:35 the query result set is successfully converted to an array. At 11:04:35, fill in the workbook [elder care] data. At 11:04:36, the workbook [unemployed] is filled in. At 11:04:36, complete the workbook [medical] data filling. At 11:04:36, fill in the workbook [Work injury] data. At 11:04:37, fill in the workbook [fertility] data. At 11:04:37, the workbook [major diseases] is filled in. At 11:04:38, fill in the workbook [supplemental medical] data. At 11:04:46, the workbook file was created.

Reply content:

There are two pages, one for uploading an EXCEL file, and the other for accepting and processing the EXCEL file. The following is the content displayed during the processing of this page, but each time the program is fully running, it will be displayed. Sometimes it takes too long to receive feedback. the user will close the page and I want to add a real-time prompt. Please give me some advice

I have thought about the asynchronous method, but the second file is in the Ifram framework, and the asynchronous retrieval also needs to wait for the value, and the final display is still the same.

// Read and insert 117 pieces of data and generate a report at 11:04:17. 11:04:31 reading and inserting the database succeeded. 11:04:31 completed adding a worksheet to the workbook. At 11:04:32, the worksheet content is written. At 11:04:32, the worksheet is cloned. At 11:04:32, the clone worksheet [unemployment] was added. At 11:04:33, the worksheet is cloned. At 11:04:33, the clone worksheet [medical care] is added. At 11:04:33, the worksheet is cloned. At 11:04:33, the clone worksheet [Work injury] is added. At 11:04:34, the worksheet is cloned. At 11:04:34, the clone worksheet [fertility] is added. At 11:04:34, the worksheet is cloned. At 11:04:34, the clone worksheet "Major Diseases" is added. At 11:04:35, the worksheet is cloned. At 11:04:35, the clone worksheet [supplemental care] is added. At 11:04:35, set the basic information of the file. 11:04:35. The query result set database is successful. 11:04:35 the query result set is successfully converted to an array. At 11:04:35, fill in the workbook [elder care] data. At 11:04:36, the workbook [unemployed] is filled in. At 11:04:36, complete the workbook [medical] data filling. At 11:04:36, fill in the workbook [Work injury] data. At 11:04:37, fill in the workbook [fertility] data. At 11:04:37, the workbook [major diseases] is filled in. At 11:04:38, fill in the workbook [supplemental medical] data. At 11:04:46, the workbook file was created.

Thank you very much for your answers. I am using a progress bar on the submit page. Press the progress bar to start activation and set a time later than the time when the user completes the operation, calculate the department that is not filled with the progress bar, and use an additional 5 S to control the progress bar speed to fill up

function flush_buffers(){    ob_end_flush();    ob_flush();    flush();    ob_start('ob_callback');}function ob_callback($buffer){    return $buffer . str_repeat(' ', max(0, 4097 - strlen($buffer)));}
Demo
Echo'
Processing 1!
'; Flush_buffers (); // do... echo'
Process 2!
'; Flush_buffers ();

Expansion:
PHP5.4 Upload progress support (Upload progress in sessions)

The subject should askReal-Time OutputGenerally, after all the php scripts are executed, the Script output will be returned at one time. However, the output content in the execution process cannot be obtained in real time. This behavior is calledOutput Cache, You can useoutput.buffering(PHP manual) to switch


  

When you access this pageHeader contentAll output1 secondAfter,At the same timeOutput12

However, you can useflushForcibly output the cached content and clear the cache

 

After flush is added,Generally,1TheheaderAfter ReturnOutput now, Output after 1 second2.

Why?GenerallyBut if the front-end web server (such as apache) has enabled gzip, deflate, and cacheRun the script only after the script is output.Will beCache again.

If you really need thisReal-Time OutputFunction, You need to disable all the caching functions on the entire response link. In fact, it is a little less than worth the candle ~

http://php.net/manual/zh/function.flush.php

There are two methods I can think of: AJAX asynchronous:

  1. Divide each step into a function, and then pass the parameter through the URL to execute the corresponding function, suchstep=firstCorresponding operationfirst()Function. Your content is returned after the operation is successful. The front end uses AJAX requests. If the request is returned successfullyappendGo to the page, and then request the next step. The next step is recursive until the task is completed. Refer to the online update plug-in I wrote for Typecho. This method is used.

  2. This method is relatively simple.appendTo a JSON file, the front-end uses AJAX to poll the request for this JSON file and display it on the page.

The upload progress is clearly stated in the blog of laruence, as @ song XiaoBei said:

1. Before PHP5.4, APC passedapc.rfc1867_freq stringOption native supports file upload cache updates, refer to http://php.net/manual/zh/apc.configuration.php#ini.apc.rfc1867
2. PECL also has extended implementation of the upload progress http://pecl.php.net/package/uploadprogress

If you use an earlier version and do not want to do the above, you can use Flash to Upload components such as swfupload or jQuery File Upload.

After the upload is complete, the processing result is displayed in real time. you can disable the various buffers of Web Server and PHP, and then rely onflush()Force output. But this is a silly practice.
A simple idea is to write a tag to a third-party storage such as memcached, redis, or even a file in each processing step. The tag is poll through ajax to update the content in the dom in quasi-real time. This should be an ideal compromise. Why?

====

@ Brief, the instructor gave me some advice. I found that his first solution split the operation into several steps for better processing. It can prevent timeout when processing large files. At the same time, pay attention to a lot of problems, such as file status maintenance to prevent leakage, and read the file repeatedly in each step... I just came to SG to have fun. I 'd like to consider the details.

Put the program in the queue for processing, put the execution log in the database or text, and then get and process progress through ajax Round Robin and display

I specifically wrote a solution article to see: http://segmentfault.com/a/1190000004235213

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.