douyueju2912 2009-06-12 01:11
浏览 28
已采纳

PHP-需要一个用于用户注册的后台站点处理的cron ...(或fork进程)

Whenever a new user signs up on my site, I want to do some pre-processing to shorten their searches in the future. This involves anywhere from 30 to 2 minutes processing time. Obviously I cannot do this when they click the submit button on signup... or on any PHP page they visit. However, I would like this done within 5 minutes of them signing up (or less).

Cron Route I THINK this needs to be in a cron job, and if so, how should I setup the cron job? If so, what should my cron line look like to run every 2 minutes, and how can I insure that I don't have the same cron job overlapping the next?

Event/Fork Route - Preferred If I can possibly throw some event to my server without disrupting my users experience or fork a process off of the users signup (instead of a cron job) how could I do this?

  • 写回答

3条回答 默认 最新

  • donglin6659 2009-06-12 01:17
    关注

    I would recommend neither solution.

    Instead, you would be best off with a long running process (daemon) that gets its jobs from a message queue. The message queue itself could be off a database if that is your preferred method.

    You will post an identifier for the job to your database, and then a long running process will iterate through them once in a while and act upon them.

    This is as simple as:

    <?php
    while(true) {
       jobs = getListOfJobsFromDatabase();  // get the jobs from the databbase
       foreach (jobs as job) {
          processAJob(job); // do whatever needs to be done for the job
          deleteJobFromDatabase(job); //remember to delete the job once its done!
       }
       sleep(60); // sleep for a while so it doesnt thrash your database when theres nothing to do
    }
    ?>
    

    And just run that script from the command line.

    The benefits of this over a cron job are that you wont get a race condition.

    You may also want to fork off the actually processing of the jobs so many can be done in parallel, rather than processing sequentially.

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

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了