How to await for tasks in progress?
dmiedev opened this issue · 2 comments
dmiedev commented
Hello, I'd like to ask a question.
I use amphp/parallel for recursive tasks -- I submit one task, then I await for the result data and submit more tasks based on this data, and so on.
Is there a good way how to await for tasks to be completed in this case? I would like to execute certain code in the main thread after the completion of all tasks before exiting the script.
Thanks in advance
kelunik commented
There are multiple primitives that can help you there, one of them is a Barrier: https://github.com/amphp/sync/blob/2.x/src/Barrier.php
Every time you enqueue a new task, you can call register there and call Barrier::await() after your initial submit.
dmiedev commented
This looks good, thank you!