workman 循环发送数据的问题。

caption

我们的业务是用户配置一个时间间隔,定时的去发送视频,图片,文字,
每个视频发送需要有个五秒的间隔,图片文字也是的。而且要求先发视频,再发图,再发文字。
请问这种怎么实现?

我目前的实现是定时器嵌套,实现的效果不是很理想,经常出现先发文字的情况 ,请问这样起定时器能够控制先发视频图片的顺序吗?
代码如下:

if(!empty($content['vedio']) && $content['vedio']){
            $vedio = $content['vedio'];
            $i = 0;
            $count = count($vedio);
            $timer_id = Timer::add(8, function()use(&$timer_id ,&$fromId, &$toId, &$vedio, &$i ,&$count)
            {
                $this->sendVideo($fromId, $toId, $vedio[$i]);
                $i++;
                if($i >= $count){
                    Timer::del($timer_id);
                }
            }); 
        }
        //图片
        if(!empty($content['pic_urls']) && $content['pic_urls']){
            $pic_urls = $content['pic_urls'];
            $i = 0;
            $count = count($pic_urls);
            $p_timer_id = Timer::add(5, function()use(&$p_timer_id ,&$fromId, &$toId, &$pic_urls, &$i ,&$count)
            {
                $this->sendImage($fromId, $toId, $pic_urls[$i]);
                $i++;
                if($i >= $count){
                    Timer::del($p_timer_id);
                }
            });
        }
        //文字
        if(!empty($content['content']) && $content['content']){
            $text = $content['content'];
            $i = 0;
            $count = count($text);
            $t_timer_id = Timer::add(3, function()use(&$t_timer_id ,&$fromId, &$toId, &$text, &$i ,&$count)
            {
                $this->sendText($fromId, $toId, $text[$i]);
                $i++;
                if($i >= $count){
                    Timer::del($t_timer_id);
                }
            });
        }
2165 1 0
1个回答

jackie_lt

按照你的代码,进程会先初始化三个定时器。视频定时器(每8s执行一次),图片定时器(5s),文字定时器(3s),注意这三个定时器会在这段代码执行的时候一次都初始化,你可以通过打印每次的timer_id发现,代码执行后三个定时器都是设置好了的。所以执行顺序就变成了3s后第一段文字,5s后第一张图片,6s后第二段文字,8s后第一个视频,9s后第三段文字,10s后第二张图片。
所以分开设置定时器似乎是不能满足需求的,可以把视频+图片+文字合起来作为一个组。每个定时器发送一组,这样就有先后顺序了。

  • caption 2019-12-16

    分组的方式也试过,没有实现他们的间隙,后来只能用sleep去实现了

年代过于久远,无法发表回答
🔝