I have a use case that I'm not sure how to solve (...
# coroutines
r
I have a use case that I'm not sure how to solve (my experience with async/concurrent code is rather simplistic). Basically I have two types of tasks: generate tasks and a combine task. I would like generate tasks to run concurrently from a queue (the generate tasks are quite CPU intensive, so I want to limit the number that run at a time). When the queue runs dry, it should then run the combine task. While the combine task is running, new generate tasks can be added to the queue, but should not run till the combine task is done. (By extension, the combine task should not run if there are any generate tasks running.) Also, there will often be long periouds of time where no tasks are running, so I don't want a busy wait for new generate tasks. What would be the correct way to handle this? Is there a common idiom or pattern for such a use case?
If it matters, the generate tasks will be writing to various files, and the combine tasks will be combining the files into a separate file. Also, I would like the ability to cancel any running generate task (they'll be writing to temp files and only copying them to the result if completed successfully) if that makes a difference.