r/tinycode • • Mar 14 '18

Map is hopefully an improvement upon the ergonomics of tools like xargs, find -exec and shell for-loops

https://github.com/soveran/map#map
34 Upvotes

9 comments sorted by

View all comments

Show parent comment

3

u/soveran Mar 15 '18

Indeed, and it also allows you to configure the max number of processes to run concurrently.

Often you can send the task to the background to run it in parallel.

For example:

printf "1\n1\n1\n" | map t 'sleep $t && say done'

If you run it that way, you will hear "done" after one second, "done" after two seconds, and "done" after three seconds.

Instead:

printf "1\n1\n1\n" | map t 'sleep $t && say done &'

This way you will probably hear a single, superimposed "done".

If you want to download files, you can take the same approach:

cat urls | map url 'curl -O $url &'

It will download the files in parallel.

1

u/IronJoeM Mar 19 '18

This crashes on my machine when giving it 100000 URLs to download.

cat urls | map url 'curl -O $url &'

This is not a problem for gnu paralel:

cat urls | parallel curl -O

1

u/soveran Mar 19 '18 edited Mar 19 '18

Yes, if creating 100k background jobs crashes your computer of course you should try a different approach! For example, you can create batches and wait for a batch to complete before starting the next one. Or you can write a short script in the language of your choice to iterate in batches, or you can just use parallel if downloading hundreds of thousands of files at once is your thing :-)

I wrote about how map compares to parallel in another thread. The summary would be that if you need to run lots of tasks in parallel very often, then it will be very useful. I wouldn't say it is tinycode because it's 8k lines of perl, but I know you are not suggesting it because it is tinycode, but because it is useful for the usecase you described, and I agree.