Laravel:任务调度[并行] [英] Laravel : Task Scheduling [ In Parallel ]

查看:486
本文介绍了Laravel:任务调度[并行]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个任务需要每隔一两个小时完成一次.所有这些都是通过Laravel使用以下命令作为cron作业安排的

I have multiple tasks that need to be done every hour or two. All of them have been scheduled via Laravel using below comamnds as cron jobs

$schedule->command('email:notifications1')
            ->cron('15 * * * * *');
$schedule->command('email:notifications2')
            ->cron('15 * * * * *');
$schedule->command('email:notifications3')
            ->cron('15 * * * * *');

问题: 以上所有任务都是非常耗时的.从结果看来,这些任务不是并行运行的.每个任务都在上一个任务结束后运行.

Issue: All of the above tasks are pretty time consuming & it seems from the results that these tasks are not running in parallel. And each tasks runs after the previous has ended.

要求 我如何并行运行它们?我希望所有的任务都在时钟滴答指定的时间后立即执行(并行).

Requirment How can i run them in parallel? I want all tasks to be executed (in parallel) as soon as the clock tick the specified time.

Laravel版本5

推荐答案

由于PHP的限制,Laravel调度程序一次只能运行一个命令.

The Laravel scheduler can only run one command at a time because of the limitations of PHP.

但是,您可以将cronjobs直接添加到crontab文件中,这样,它们将在单独的进程中并行执行.

You could, however, add the cronjobs directly in your crontab file, this way, they will be executed in parallel in separate processes.

15 * * * * * php /path/to/artisan email:notifications1
15 * * * * * php /path/to/artisan email:notifications2
15 * * * * * php /path/to/artisan email:notifications3


解决此问题的另一种方法是让作业在其他时间开始.由于cron作业每分钟都会启动一个新的php进程,因此它们不会互相影响.


Another way to fix this is to let the jobs start at another time. Because a new php process is started every minute by the cron job, these do not affect each other.

例如:

$schedule->command('email:notifications1')
            ->cron('5 * * * * *');
$schedule->command('email:notifications2')
            ->cron('10 * * * * *');
$schedule->command('email:notifications3')
            ->cron('15 * * * * *');

这篇关于Laravel:任务调度[并行]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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