omnilight / yii2-scheduling

Scheduling extension for Yii2 framework
MIT License
315 stars 83 forks source link

Can circular tasks be asynchronous? #58

Open siyecoo opened 4 years ago

siyecoo commented 4 years ago

$scheduleList = Schedule::find()->where(['is_enabled' => 1, 'task_type' => 1])->asArray()->all();

if (empty($scheduleList)) exit('无效用户队列!!');

foreach ($scheduleList as $key => $item) { $schedule->command($command)->cron($item['execute']);//Synchronous block }

kstkn commented 4 years ago

Not sure I fully understand the question. The statement $schedule->command($command)->cron('...') itself is asynchronous Each event though will block execution if it doesn't have callbacks. See \omnilight\scheduling\Event::run

siyecoo commented 4 years ago

@kstkn $schedule->command('order1')->cron(' '); // method order1 needs sleep 3s synchronization block In addition, order2,command execution will block $schedule->command('order2')->cron(' ');

kstkn commented 4 years ago

Sorry, @siyecaoya, I still don't get it. I'll try to make an assumption that what your issue is that you want two commands (events) to be executed asynchronously. In current implementation that is only possible to achieve using callbacks, see example:

 $schedule->command('first')->cron('* * * * *')->then(function() {});
 $schedule->command('second')->cron('* * * * *')->then(function() {});

With callbacks (then) method runCommandInBackground will be used, hence command execution will not be blocking.

I hope that helps :)

If not, pls, explain what are you trying to do or wait until other people comment

siyecoo commented 4 years ago

@kstkn Thank you very much for your guidance.

kstkn commented 4 years ago

no problems, @siyecaoya. Does this answer your question?

siyecoo commented 4 years ago

@kstkn Should not. The business scenario is limited. I need to verify it

JimChenWYU commented 4 years ago

it use exec to run command, so just let it run in background, it will be asynchronous. https://www.php.net/manual/en/function.exec.php#86329