workman-json-rpc调用服务时好时坏,异常recvData empty,无报错!急!!!

0

用workman-json-rpc做了一个微信发送大量模板消息的程序。客户端代码如下


            $address_array = array(
'tcp://127.0.0.1:2015'
);
// 配置服务端列表
RpcClient::config($address_array);
$Weixin = RpcClient::instance('Weixin');

$Common = new Common;
//获取公众号消息模板ID
$templateid = $Common->configsFromName("wxmbtzid");
//获取是先发现模板消息还是先发送客服消息
$usekfmessage = $Common->configsFromName("usekfmessage");

$config = get_addon_config('third');
$wxconfig = $config["wechat"];
$appid = $wxconfig["app_id"];

$access_token = getAccessToken();
//发送消息
foreach ($param as $k => $value){
$Weixin->asend_sendMsg($usekfmessage,$templateid,$access_token,$value);
}

//接受消息,断开连接
foreach ($param as $k => $value){
$data[$k] = $Weixin->arecv_sendMsg($usekfmessage,$templateid,$access_token,$value);
}

我做了发现WORKERMAN执行经常报错recvData empty。

然后在服务端查看状态

发现有一条连接断开了。这种需要怎么处理,我现在才发送了4条连接,就断开了一条。我需要的是一次性要发送1000条以上才行。请问大家有没有什么好的办法

已邀请:

walkor

赞同来自:

recvData empty 说明业务执行太慢了,超时了。
如果你的业务很慢,不建议用JsonRpc,用消息队列更好一些。

walkor

赞同来自:

有时候一些项目运行在apache或者php-fpm环境,无法使用workerman/redis-queue项目,可以参考如下函数实现发送


function redis_queue_send($redis, $queue, $data, $delay = 0) {
$queue_waiting = 'redis-queue-waiting';
$queue_delay = 'redis-queue-delayed';
$now = time();
$package_str = json_encode([
'id' => rand(),
'time' => $now,
'delay' => 0,
'attempts' => 0,
'queue' => $queue,
'data' => $data
]);
if ($delay) {
return $redis->zAdd($queue_delay, $now + $delay, $package_str);
}
return $redis->lPush($queue_waiting.$queue, $package_str);
}

其中,参数$redis为redis实例。例如redis扩展用法类似如下:


$redis = new Redis;
$redis->connect('127.0.0.1', 6379);
$queue = 'user-1';
$data= ['some', 'data'];
redis_queue_send($redis, $queue, $data);

花儿

赞同来自:

这个是我原来的一个订阅的。
$worker->onWorkerStart = function () {
$client = new Client('redis://127.0.0.1:6379');
$client->subscribe('sendmsg', function($data){
$Weixin = new Weixin();
$starttime = time();
foreach ($data as $k => $value){
$result = $Weixin->sendMsg($value);
var_dump($result);
}
$endtime = time();
echo $endtime-$starttime;
});
};
按照上面的说法,我需要发布10条订阅吗?,代码如下
$worker->onWorkerStart = function () {
$client = new Client('redis://127.0.0.1:6379');
$client->subscribe('sendmsg', function($data){
$Weixin = new Weixin();
$starttime = time();
foreach ($data as $k => $value){
$result = $Weixin->sendMsg($value);
var_dump($result);
}
$endtime = time();
echo $endtime-$starttime;
});


$client->subscribe('sendmsg2', function($data){
$Weixin = new Weixin();
$starttime = time();
foreach ($data as $k => $value){
$result = $Weixin->sendMsg($value);
var_dump($result);
}
$endtime = time();
echo $endtime-$starttime;
});

};

要回复问题请先登录注册