长时间运行脚本的可突发输出 [英] Burstable Output for Long running scripts

查看:91
本文介绍了长时间运行脚本的可突发输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

脚本

<?php
include('time.php'); //time script
echo "First 100 users of SO<br/>";
for($i=0; $i<100;$i++){
$contents=file_get_contents("https://stackoverflow.com/privileges/user/".$i);
preg_match('!<div class="summarycount al">(.+?)</div>!', $contents, $matches);
$rep = $matches[1];
echo "<br/>".$i.") ".$rep."<br/>";

include('timetaken.php'); //script which outputs time difference
}
?>

输出

First 100 users of SO

0) 0
2.3584280014038
1) 14,436
4.469074010849
2) 875
10.651238918304
3) 2,431
12.991086959839
4) 8,611
15.451638936996
5) 14,988
17.535580873489
6) 0
19.686461925507
7) 0
21.796992063522
8) 218
23.931365013123
9) 2,569
26.419286966324
10) 101
28.540382862091
11) 232
30.755586862564
12) 0
32.960548877716
13) 33,898
35.163224935532
14) 0
37.280658006668
15) 6,388
39.425274848938
16) 143
41.541421890259
17) 14,366
43.655340909958
18) 0
45.771246910095
19) 99
47.882269859314
20) 4,204
49.993322849274
21) 0
52.108762979507
22) 1,517
54.221307039261
23) 411
56.345490932465
24) 103
58.892389059067
Fatal error: Maximum execution time of 60 seconds exceeded in C:\test.php on line 5

此脚本存在问题: 1.页面在超时60秒后加载

Problems with this script: 1. The page loads after 60 seconds when it timesout

我知道我可以添加

set_time_limit(500);

获取代码,并获得前100个信誉,但这将导致页面加载(例如120秒).

to the code and get the first 100 reputations, but that will result in a page load after say 120seconds.

当使用PHP或使用任何其他语言(python,java)或其他任何方式收集数据时,如何在短时间内获得结果. 在有人说之前,我已经阅读了管理长期运行的php的最佳方法脚本?,可能是重复的脚本,但没有回答我的问题. 我的问题不是完成全部工作,而是在完成时显示结果.

How can I get the result in short bursts, as the data is gathered using PHP or using any other language (python, java) or anything else. Before someone says it, I have read Best way to manage long-running php script?, which may be a possible duplicate , but does not answer my question. My question is not completing the entire job, but displaying the results as it is being done.

(请为我处理标签)

推荐答案

如果样本确实在做,那么如何收集数据并将其存储在本地?那会大大加快速度.您可以定期进行收割,但是除非数据真正具有时间敏感性,否则可能并不是您希望在每次页面加载时都执行此操作.

If the sample is really what you're doing, how about harvesting data, and storing it locally? That'd speed things up a lot. You can periodically reharvest, but unless the data's really time sensitive, this probably isn't something you want to do on every page load.

或者,我会认真考虑将一些逻辑移交给客户端.这是两种方法:

Alternatively, I'd seriously consider moving some logic over to the client. Here's two approaches:

1)有一个php进程,该进程以get参数开始记录.对该进程进行ajax调用,每个进程都获取下一组记录,并将结果添加到dom中. (JQuery是执行此操作的一种好方法.)根据您的需求以及测试显示的内容,您可以为每个get启动下一个get请求启动回调,也可以一次启动多个get.

1) Have a php process which takes a get parameter for record to start with. Make ajax calls to that process with each one grabbing the next set of records, and adding the result to the dom. (JQuery's one good way to do this.) Depending on your needs, and what testing shows, you could have the callback for each get launch the next get request, or you could launch several gets at a time.

2)完全跳过php,并在浏览器中使用javascript进行所有操作.毕竟,您只是加载和解析html(尽管您可能必须处理一些跨域问题).

2) Skip php entirely, and do everything with javascript in the browser. After all, you're just loading and parsing html (although you might have to deal with some cross-domain issues).

这篇关于长时间运行脚本的可突发输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆