#Rust parallel tasks manager

8 messages · Page 1 of 1 (latest)

arctic sun
#

Hey there. I was intending to build something like a Makefile clone for myself (a structured task runner) with specific functionalities and I found that first I should build the task runner. I'm more used to writing things in Go, I started doing it there, as the garbage collection would diretly help me just think of the logic. The Go logic was: Each task has a key and a run function, which will be run one in each Go Routine. Each tasks may have dependencies. If task B depends on task A to run, there will be a channel shared between them. When A finishes, it sends a message to that channel. B will be waiting for a message on that channel. Multiple dependencies mean multiple channels for the tasks to wait. One may notice this is a clear Ownership problem: since I am using a map to store the tasks by key, I am mutating 2 different entries of that map at the same time.

Finally, for my question: What am I doing wrong? Is this sort of a "this logic just works in Go because of the language runtime" thing? Or can I do the same thing in Rust, with the same logic?

Go code: https://gist.github.com/FulecoRafa/1b3f80afc282ade3345a1830aef9c9e8
Rust code (or what remains of it after I scrambled it up trying to make it work on my own): https://gist.github.com/FulecoRafa/8b85c5c5d94355385e5f3a7fc40417f9

Thanks in advance! 🙂

rocky axle
#

broadly, the optimal way to handle things like this in Rust is to split up the ownership so there is not a conflict

#

or to arrange for shared ownership when necessary

#

but I'm a little confused about your description and your code

#

I think maybe the strategy you need here is:
separate running the tasks from looking at the result of them

#

there can be a map of task key to result channels/whatever, which only needs to be mutated when the task is registered, but doesn't own the task data itself, so running the task does not hold the mutex

#

but also I see you are using oneshot channels but also want multiple dependencies