dongxin0031 2017-10-24 14:43
浏览 25
已采纳

Laravel同步发射功能

I have such function in my trait:

public function cupPlayMatch(Season $season, $round_id)
{

    foreach($season->cups as $cup)
    {
        $this->cupPlay($cup, $round_id);
    }

}

And second cup isstarting playing when first cup finished. How I can start playing all my cups simultaneous?

  • 写回答

1条回答 默认 最新

  • dongmu3457 2017-10-24 15:06
    关注

    For the most part, PHP is "synchronous", that means that you theoretically can't make "simultaneous calls" to any function.

    However, some workarounds exist to make this work.

    PHP is a script language. So when you launch this in your console:

    php -r "echo 'Hello World';"
    

    A PHP process is launched, and anything that happen in this process is executed synchronously.

    So the solution here, is to launch various PHP processes in order to be able to run functions simultaneously.

    Imagine an SQL table where you put all the functions that you want to execute simultaneously. You could then run 10 php processes that would actually work "at the same time".

    Laravel provides a solution to this problem out of the box. And as @Anton Gildebrand mentioned it in the comments, it's called "Queues".

    You can find the documentation here: https://laravel.com/docs/5.5/queues

    The laravel way of doing it, is to create "jobs". Each Job represents a function that you want to execute. Here, your Job would be cupPlay.

    Here is the basic example of a job copy pasted from the documentation:

    <?php
    
    namespace App\Jobs;
    
    use App\Podcast;
    use App\AudioProcessor;
    use Illuminate\Bus\Queueable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    
    class ProcessPodcast implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        protected $podcast;
    
        /**
         * Create a new job instance.
         *
         * @param  Podcast  $podcast
         * @return void
         */
        public function __construct(Podcast $podcast)
        {
            $this->podcast = $podcast;
        }
    
        /**
         * Execute the job.
         *
         * @param  AudioProcessor  $processor
         * @return void
         */
        public function handle(AudioProcessor $processor)
        {
            // Process uploaded podcast...
        }
    }
    

    When you'll have configured your worker driver to run your queues, you'll just need to launch:

    php artisan queue:work --queue=high,default
    

    from the command line and it will execute your tasks.

    And you can execute as many workers as you want, depending on your needs...

    I hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站