Let's say I have the following code :
(defn multiple-writes []
(doseq [[x y] (map list [1 2] [3 4])] ;; let's imagine those are paths to files
(when-not (exists? x y) ;; could be left off, I feel it is faster to check before overwriting
(write-to-disk! (do-something x y)))))
That I call like this (parameters omitted) :
(go (multiple-writes))
I use go
to execute some code "in the background", but I do not know if I am using the right tool here. Some more information about those functions :
- this is not high-priority code at all. It could even fail -
multiple-writes
could be seen as a cache-filling function. - I consequently do not care about the return value.
do-something
takes a between 100 and 500 milliseconds depending of the inputdo-something
consumes some memory (uses image buffers, some images can be 2000px * 2000px)- there are 10 to 40 elements/images to be processed every time
multiple-writes
is called. - every call to
write-to-disk
will create a new file (or overwrite it if any, though that should not happen) write-to-disk
writes always in the same directory
So I would like to speed up things by executing (write-to-disk! (do-something x y))
in parallel to go as fast as possible. But I don't want to overload the system at all, since this is not a high-priority task.
How should I go about this ?
Note : despite the title, this is not a duplicate of this question since I don't want to restrict to 3 threads (not saying that the answer can't be the same, but I feel this question differs).