0

这是我试图在高级伪代码中完成的事情:

query db for a list of names (~100)
for each name (using php) {
    query a 3rd party site for xml based on the name
    parse/trim the data received
    update my db with this data
    Wait 15 seconds (the 3rd party site has restrictions and I can only make 4 queries / minute)
}

所以这运行良好。整个脚本耗时约 25 分钟(99% 的时间花在每次迭代后等待 15 秒)。然后我的虚拟主机进行了更改,以便脚本在 70 秒后超时(可以理解)。这完全破坏了我的脚本。

我假设我需要使用 cronjobs 或命令行来完成此操作。我只了解 cronjobs 的基本用法。关于如何在 cronjob 中拆分这项工作的任何高级建议?我不确定 cronjob 如何解析动态列表。

4

2 回答 2

0

简而言之,没有什么不同。您将通过命令行执行它,而不是通过 modphp 或 fcgi 执行脚本php /path/to/script.php

因为这是和http不同的环境,所以有些东西显然是行不通的。会话、cookies、get 和 post 变量。输出被发送到标准输出而不是浏览器。

您可以使用$argv.

于 2013-06-04T17:42:19.083 回答
0

cron itself has no idea of your list and what is done already, but you can use two kinds of cron-jobs.

The first cron-job - that runs for example once a day - could add your 100 items to a job queue.

The second cron-job - that runs for example once every minute in a certain period - can check if there are items in the queue, execute one (or a few) and remove it from the queue.

Note that both cron-jobs are just triggers to start a php script in this case and you have two different scripts, one to set the queue and one to process part of a queue so almost everything is still done in php.

于 2013-06-04T17:41:34.470 回答