doushang7209 2013-06-19 13:28
浏览 72
已采纳

在脚本之间实现共享变量或如何实现服务器端进度的正确方法?

I have a long job serving to fill a table in a db. This job may takes various time to finish. So, I need a progress bar. Just counting on the page is enough. As far I found several solutions:

  1. Using _SESSION. OK, it works on most of servers I used, but I have many problem with ERR_RESPONSE_HEADERS_TOO_BIG error on chrome and sometimes - "headers already sent" on some servers, caused by session_start/session_write_close called in a loop. I loved this method but now I have reject it
  2. Using kind of ob_start/ob_end_flush inside of the long operation's loop. This doesn't work. It just displays all buffered echo-ed messaged after the end of script.
  3. Using db to store a shared variable (not tried yet)
  4. Using separate (temporary) file. (not tried yet).

I have already implemented infrastructure: one script for long job (populating a table in db) and another one used by JS to read shared variable containing progress status. Both these scripts I called with jQuery .ajax method expecting the results in .done function.

Is there any other methods? It seems I will like to use 4) variant, but I want to know what is the usual practice for implementing progress bar.

Thanks:)

  • 写回答

3条回答 默认 最新

  • dongwen9947 2013-06-19 13:37
    关注

    Firstly, if you aren't already, I would run the PHP code as a separate process.

     <?php 
    
     exec('/usr/bin/php /Path/to/script.php  {$param1} {$param2} > /dev/null 2>/dev/null &'); 
    
     ?>
    

    I would then get this process to update a DB with its progress against the ID of the user, you could then use a ajax call inside a timeout function to get the status (how many DB writes complete, how many left, etc) from the DB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)