Some Bits: Nelson's log

task-spooler

This Hacker News discussion taught me about a great new-to-me tool: Duc Nguyen’s task-spooler. It lets you queue up a set of jobs on a Linux box and have them run in parallel in the background. It’s available in Ubuntu and has the shell command tsp.

I’ve used GNU Parallel before for this and it’s great but the model there is you run one big command and the jobs run in parallel all together. The queueing interface in task-spooler means you can keep throwing work at the system and it gets done eventually. Task queues are one of the great computing paradigms IMHO, along with event publish/subscribe.

The interface is very easy. You run tsp command to enqueue a new command, it returns immediately. It sets up a background process to run the command if there isn’t already one and puts the job on the queue. The default parallelism is only 1; you can run tsp -S 4 if you want to run 4 jobs in parallel. Output is written to files in /tmp by default.

There’s more complex commands for querying job status, etc. There’s minimal support for more complex queue management. including options for “wait for a job” and “the job will be run only if the job of given id ends well”. I wouldn’t use this to replace a complex task queue system but it’s nice to have those basics available.

$ tsp -K
$ ps xu | grep tsp
nelson    3983  0.0  0.0   6304   652 pts/0    S+   11:45   0:00 grep tsp

$ tsp sh -c "sleep 1; date"
0
$ tsp
ID   State      Output               E-Level  Times(r/u/s)   Command [run=0/1]
0    finished   /tmp/ts-out.H7AJLG   0        1.00/0.00/0.00 sh -c sleep 1; date
$ cat /tmp/ts-out.H7AJLG
Mon 21 Aug 2023 11:45:47 AM PDT

$ !ps
ps xu | grep tsp
nelson    3985  0.0  0.0   2548    84 ?        Ss   11:45   0:00 tsp sh -c sleep 1; date
nelson    3999  0.0  0.0   6304   648 pts/0    S+   11:46   0:00 grep tsp

$ tsp -K
$ tsp -S 3
$ date; for i in {1..6}; do tsp sh -c "sleep 5; date"; done
Mon 21 Aug 2023 11:54:10 AM PDT
0
1
2
3
4
5

$ tsp
ID   State      Output               E-Level  Times(r/u/s)   Command [run=3/3]
0    running    /tmp/ts-out.Ifxnc9                           sh -c sleep 5; date
1    running    /tmp/ts-out.estpmo                           sh -c sleep 5; date
2    running    /tmp/ts-out.vDtaPG                           sh -c sleep 5; date
3    queued     (file)                                       sh -c sleep 5; date
4    queued     (file)                                       sh -c sleep 5; date
5    queued     (file)                                       sh -c sleep 5; date

$ tsp
ID   State      Output               E-Level  Times(r/u/s)   Command [run=3/3]
3    running    /tmp/ts-out.IgWMfx                           sh -c sleep 5; date
4    running    /tmp/ts-out.tSSCYL                           sh -c sleep 5; date
5    running    /tmp/ts-out.j003cY                           sh -c sleep 5; date
0    finished   /tmp/ts-out.Ifxnc9   0        5.00/0.00/0.00 sh -c sleep 5; date
1    finished   /tmp/ts-out.estpmo   0        5.00/0.00/0.00 sh -c sleep 5; date
2    finished   /tmp/ts-out.vDtaPG   0        5.00/0.00/0.00 sh -c sleep 5; date
$ tsp -t

Mon 21 Aug 2023 11:54:20 AM PDT