Last active
November 7, 2022 16:16
-
-
Save cpelley/da3f3f43e2b3fb2288a78dd659d1d59a to your computer and use it in GitHub Desktop.
Testing gnu parallel behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| import sys | |
| from time import sleep | |
| import random | |
| for i in range(random.randint(3, 8)): | |
| sleep(random.random() / 10) | |
| print(f"{sys.argv[1]}, step {i}") | |
| if i == 2 and ("arg1" in sys.argv[1] or "arg2" in sys.argv[1]): | |
| raise ValueError(f"{sys.argv[1]} raised an exception") |
Author
Author
Removing the commands wrapping the call to random_duration_script.py confirms that the error code is otherwise preserved even when processes complete successfully following a failing process:
$ parallel './random_duration_script.py {}' ::: arg1 arg2 arg3 arg4 arg5 2&> /dev/null; echo $?
2(note that I pipe stdout and stderr to /dev/null here as it is unimportant in this exploration of error code handling of 'parallel')
Author
We can retain our error code AND still echo our 'endgroup' with a minor change:
$ parallel 'echo ::group::{}; ./random_duration_script.py {} && echo ::endgroup:: || (echo ::endgroup:: && exit 1) ' ::: arg1 arg2 arg3 arg4 arg5 2&> /dev/null; echo $?
2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing how parallel deals with collecting stdout, stderr and error codes in the case where we require running multiple commands on each process (in this case an echo header and footer as per our requirement to use parallel as part of github actions).
Note how stderr is output after stdout but remains ordered as we expect.
However, we see that the error code is lost in this case due to the echo immediately following the exception raised by
random_duration_script.py.