来自用PHP编写的守护程序的mysql连接 [英] mysql connection from daemon written in php

查看:69
本文介绍了来自用PHP编写的守护程序的mysql连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个守护进程,可以从mysql获取一些东西,并根据mysql的信息发出一些curl请求。由于我精通php,因此我使用pear的System_Daemon在php中编写了该守护程序。

i have written a daemon to fetch some stuff from mysql and make some curl requests based on the info from mysql. since i'm fluent in php i've written this daemon in php using System_Daemon from pear.

这很好,但是我很好奇连接mysql的最佳方法。每隔几秒钟就创建一个新的mysql连接感到很奇怪,我应该尝试一个持久连接吗?还有其他输入吗?尽可能将潜在的内存泄漏保持在最低水平……

this works fine but i'm curious about the best approach for connecting to mysql. feels weird to create a new mysql connection every couple of seconds, should i try a persistent connection? any other input? keeping potential memory leaks to a minimum is of the essence...

清理脚本,如下所示。暂时删除了mysql内容,使用了一个虚拟数组来保持此状态不变:

cleaned up the script, attached below. removed the mysql stuff for now, using a dummy array to keep this unbiased:

#!/usr/bin/php -q
<?php
require_once "System/Daemon.php";

System_Daemon::setOption("appName", "smsq");
System_Daemon::start();

$runningOkay = true;

while(!System_Daemon::isDying() && $runningOkay){

    $runningOkay = true;
    if (!$runningOkay) {
        System_Daemon::err('smsq() produced an error, '.
            'so this will be my last run');
    }

    $messages = get_outgoing();
    $messages = call_api($messages);
    #print_r($messages);

    System_Daemon::iterate(2);
}

System_Daemon::stop();  

function get_outgoing(){ # get 10 rows from a mysql table
    # dummycode, this should come from mysql
    for($i=0;$i<5;$i++){
        $message->msisdn = '070910507'.$i;
        $message->text = 'nr'.$i;
        $messages[] = $message;
        unset($message);
    }
    return $messages;
}

function call_api($messages=array()){
    if(count($messages)<=0){
        return false;
    }else{
        foreach($messages as $message){
            $message->curlhandle = curl_init();
            curl_setopt($message->curlhandle,CURLOPT_URL,'http://yadayada.com/date.php?text='.$message->text);
            curl_setopt($message->curlhandle,CURLOPT_HEADER,0);
            curl_setopt($message->curlhandle,CURLOPT_RETURNTRANSFER,1);
        }
        $mh = curl_multi_init();
        foreach($messages as $message){
            curl_multi_add_handle($mh,$message->curlhandle);
        }
        $running = null;
        do{
            curl_multi_exec($mh,$running);
         }while($running > 0);
        foreach($messages as $message){
            $message->api_response = curl_multi_getcontent($message->curlhandle);
            curl_multi_remove_handle($mh,$message->curlhandle);
            unset($message->curlhandle);
        }
        curl_multi_close($mh);
    }
    return $messages;
} 


推荐答案

从技术上讲,如果它是守护程序,它会在后台运行,直到您要求时才会停止。在这种情况下,无需使用持久连接,甚至您也可以不使用。

Technically if it's a daemon, it runs in background and doesn't stop until you ask it to. There's is no need to use a persistent connection in that case, and even, you probably shouldn't. I'd expect the connection to close when I kill the daemon.

基本上,您应该在启动时打开连接,然后在关机时关闭连接,仅此而已。但是,您应该在其中放置一些错误陷阱,以防守护程序运行时连接意外断开,因此它可以正常关闭(通过在某个位置记录连接断开)或稍后重试重新连接。

Basically, you should open a connection on startup, and close it on shutdown, and that's about it. However, you should put some error trapping in there in case the connection drops unexpectedly while the daemon is running, so it either shutdowns gracefully (by logging a connection drop somewhere) or have it retry a reconnection later.

这篇关于来自用PHP编写的守护程序的mysql连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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