multiCMD 1.72__tar.gz → 1.73__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. {multicmd-1.72 → multicmd-1.73}/PKG-INFO +36 -7
  2. {multicmd-1.72 → multicmd-1.73}/README.md +35 -6
  3. {multicmd-1.72 → multicmd-1.73}/multiCMD.egg-info/PKG-INFO +36 -7
  4. {multicmd-1.72 → multicmd-1.73}/multiCMD.py +144 -38
  5. {multicmd-1.72 → multicmd-1.73}/tests/test_async_executor.py +427 -26
  6. {multicmd-1.72 → multicmd-1.73}/tests/test_interrupts.py +10 -10
  7. {multicmd-1.72 → multicmd-1.73}/tests/test_process_groups.py +4 -4
  8. {multicmd-1.72 → multicmd-1.73}/tests/test_reader.py +1 -1
  9. {multicmd-1.72 → multicmd-1.73}/tests/test_sudo.py +8 -2
  10. {multicmd-1.72 → multicmd-1.73}/tests/test_supervision.py +5 -5
  11. {multicmd-1.72 → multicmd-1.73}/LICENSE +0 -0
  12. {multicmd-1.72 → multicmd-1.73}/multiCMD.egg-info/SOURCES.txt +0 -0
  13. {multicmd-1.72 → multicmd-1.73}/multiCMD.egg-info/dependency_links.txt +0 -0
  14. {multicmd-1.72 → multicmd-1.73}/multiCMD.egg-info/entry_points.txt +0 -0
  15. {multicmd-1.72 → multicmd-1.73}/multiCMD.egg-info/top_level.txt +0 -0
  16. {multicmd-1.72 → multicmd-1.73}/scripts/multiCMD +0 -0
  17. {multicmd-1.72 → multicmd-1.73}/setup.cfg +0 -0
  18. {multicmd-1.72 → multicmd-1.73}/setup.py +0 -0
  19. {multicmd-1.72 → multicmd-1.73}/tests/test_api.py +0 -0
  20. {multicmd-1.72 → multicmd-1.73}/tests/test_cli.py +0 -0
  21. {multicmd-1.72 → multicmd-1.73}/tests/test_cli_closed_stdout.py +0 -0
  22. {multicmd-1.72 → multicmd-1.73}/tests/test_cli_failed_stdout.py +0 -0
  23. {multicmd-1.72 → multicmd-1.73}/tests/test_cli_input.py +0 -0
  24. {multicmd-1.72 → multicmd-1.73}/tests/test_cli_summary.py +0 -0
  25. {multicmd-1.72 → multicmd-1.73}/tests/test_display.py +0 -0
  26. {multicmd-1.72 → multicmd-1.73}/tests/test_embed.py +0 -0
  27. {multicmd-1.72 → multicmd-1.73}/tests/test_env.py +0 -0
  28. {multicmd-1.72 → multicmd-1.73}/tests/test_expand_ranges.py +0 -0
  29. {multicmd-1.72 → multicmd-1.73}/tests/test_handover.py +0 -0
  30. {multicmd-1.72 → multicmd-1.73}/tests/test_helpers.py +0 -0
  31. {multicmd-1.72 → multicmd-1.73}/tests/test_packaging.py +0 -0
  32. {multicmd-1.72 → multicmd-1.73}/tests/test_parsing.py +0 -0
  33. {multicmd-1.72 → multicmd-1.73}/tests/test_pass_fds.py +0 -0
  34. {multicmd-1.72 → multicmd-1.73}/tests/test_ping.py +0 -0
  35. {multicmd-1.72 → multicmd-1.73}/tests/test_pool.py +0 -0
  36. {multicmd-1.72 → multicmd-1.73}/tests/test_pty.py +0 -0
  37. {multicmd-1.72 → multicmd-1.73}/tests/test_reader_quit.py +0 -0
  38. {multicmd-1.72 → multicmd-1.73}/tests/test_regressions.py +0 -0
  39. {multicmd-1.72 → multicmd-1.73}/tests/test_run_command.py +0 -0
  40. {multicmd-1.72 → multicmd-1.73}/tests/test_run_commands.py +0 -0
  41. {multicmd-1.72 → multicmd-1.73}/tests/test_task.py +0 -0
  42. {multicmd-1.72 → multicmd-1.73}/tests/test_terminal_width.py +0 -0
  43. {multicmd-1.72 → multicmd-1.73}/tests/test_windows.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.72
3
+ Version: 1.73
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -405,10 +405,19 @@ print(ex.get_results())
405
405
  seconds (the constructor's `timeout` is only the per-command inactivity timeout).
406
406
  `AsyncExecutor` takes the same `shell`, `prefix`, `color` and `pty` options as
407
407
  `run_commands`, as constructor defaults or per call. One dispatcher thread
408
- starts each queued command only after taking a permit from the semaphore, so the
409
- executor holds at most one thread per permit plus the dispatcher, however many
410
- commands are queued. A command's `task.thread` is `None` until it starts, and goes
411
- back to `None` once it has finished and the executor has joined its thread, so a
408
+ starts each queued command only after taking a permit from the semaphore, so with a
409
+ limit the executor holds at most one thread per permit plus the dispatcher, however many
410
+ commands are queued. An executor given no semaphore, with `max_threads=0` (or less, or
411
+ `inf`), has no limit, as for `run_commands`. A `max_threads` given to one call limits that
412
+ call within the semaphore it uses (the executor's, or a `sem` given to the call; with
413
+ `sem=None`, the call's own `max_threads`, or the executor's `max_threads` when it gives
414
+ none), never past it, and holds back no command of another call. A call's commands start
415
+ in their order. A `max_threads` below 1 (or `inf`) is no limit; any other value that is
416
+ not a whole number is a `TypeError`, in a call (`None` there means not given) and in a
417
+ constructor that makes the semaphore.
418
+ A command's `task.thread`
419
+ is `None` until it starts, and goes back to `None` once it has finished and the
420
+ executor has joined its thread, so a
412
421
  large batch does not keep a `Thread` object per finished command. `stop()` skips
413
422
  commands that have not started yet, and so does a Ctrl-C ( or an error ) that interrupts
414
423
  `run_command(s)`, for that call's commands.
@@ -578,6 +587,26 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
578
587
  `scripts/realsudo.py` runs `--sudo` against real sudo (1.8.23, 1.9.5, 1.9.9, 1.9.16) in Docker, with pam_faillock on, and
579
588
  prints a table of what each version of `multiCMD.py` given to it did (`python3 scripts/realsudo.py` says how).
580
589
 
590
+ ## Upgrading to 1.73 from 1.72
591
+
592
+ - Nothing needs doing unless a program builds an `AsyncExecutor` with no `semaphore` argument and a `max_threads` of 0 or
593
+ less, or above 1 that is not whole and not `inf` (`1.5`), or with `semaphore=None`, or gives `max_threads` to its
594
+ `run_command(s)`. `mcmd` and the module-level `run_command(s)` are unchanged.
595
+ - Fix: `AsyncExecutor(max_threads=0)`, or a negative value, with no `semaphore` given, runs every command at once, as
596
+ `run_commands(max_threads=0)` and `mcmd -m 0` do (1.72 started nothing, with no error, and `join()` never returned; a
597
+ negative value raised `ValueError`).
598
+ - Changed: a `max_threads` given to an executor's `run_command(s)` limits that call within the semaphore it uses (the
599
+ executor's, or a `sem` given to the call): it can lower the limit for the call, never raise it, and 0 or less adds none
600
+ (1.72 ignored it there, so `max_threads=1` on an executor of four now runs that call one at a time). It holds back no
601
+ command of another call. A call given a `sem` is limited by that one instead of the executor's, and `sem=None` by its
602
+ `max_threads`, or the executor's when it gives none, as before; with `semaphore=None` on the executor the calls no longer
603
+ wait for each other (1.72 made a call's limit hold up the calls queued after it).
604
+ - Changed: a `max_threads` given to an executor's call, or to its constructor when it makes the semaphore, that is not a
605
+ whole number (`1.5`) raises `TypeError` (1.72 made a semaphore of it that never blocks); below 1 (`0.5` too) and `inf` are
606
+ no limit. A string given to a call raises `TypeError` before anything runs, where 1.72 ignored it under the executor's
607
+ semaphore. `None` given to a call is the same as not giving it (1.72 raised `TypeError` for it on a call with no
608
+ semaphore).
609
+
581
610
  ## Upgrading to 1.72 from 1.71
582
611
 
583
612
  - Nothing needs doing. Runs on a terminal, and runs whose `sudo -v` signs in (NOPASSWD for all, an askpass helper, a
@@ -592,8 +621,8 @@ prints a table of what each version of `multiCMD.py` given to it did (`python3 s
592
621
  specified`). `--dry-run` with no terminal shows plain `sudo`: `sudo -v` does not run in a dry run.
593
622
  - Changed, Windows (off POSIX): no `sudo -v` runs; the commands stay plain `sudo` (1.70 and 1.71 ran a `sudo -v` there and
594
623
  ignored its exit).
595
- - Known, planned for 1.73 ([docs/limitations.md](docs/limitations.md)): `AsyncExecutor(max_threads=0)` starts no command,
596
- and a per-call `max_threads` is ignored while the executor's own semaphore is used (as in 1.70 and 1.71).
624
+ - Known in 1.72, fixed in 1.73 (see above): `AsyncExecutor(max_threads=0)` starts no command, and a per-call
625
+ `max_threads` is ignored while the executor's own semaphore is used (as in 1.70 and 1.71).
597
626
 
598
627
  ## Upgrading to 1.71 from 1.70
599
628
 
@@ -380,10 +380,19 @@ print(ex.get_results())
380
380
  seconds (the constructor's `timeout` is only the per-command inactivity timeout).
381
381
  `AsyncExecutor` takes the same `shell`, `prefix`, `color` and `pty` options as
382
382
  `run_commands`, as constructor defaults or per call. One dispatcher thread
383
- starts each queued command only after taking a permit from the semaphore, so the
384
- executor holds at most one thread per permit plus the dispatcher, however many
385
- commands are queued. A command's `task.thread` is `None` until it starts, and goes
386
- back to `None` once it has finished and the executor has joined its thread, so a
383
+ starts each queued command only after taking a permit from the semaphore, so with a
384
+ limit the executor holds at most one thread per permit plus the dispatcher, however many
385
+ commands are queued. An executor given no semaphore, with `max_threads=0` (or less, or
386
+ `inf`), has no limit, as for `run_commands`. A `max_threads` given to one call limits that
387
+ call within the semaphore it uses (the executor's, or a `sem` given to the call; with
388
+ `sem=None`, the call's own `max_threads`, or the executor's `max_threads` when it gives
389
+ none), never past it, and holds back no command of another call. A call's commands start
390
+ in their order. A `max_threads` below 1 (or `inf`) is no limit; any other value that is
391
+ not a whole number is a `TypeError`, in a call (`None` there means not given) and in a
392
+ constructor that makes the semaphore.
393
+ A command's `task.thread`
394
+ is `None` until it starts, and goes back to `None` once it has finished and the
395
+ executor has joined its thread, so a
387
396
  large batch does not keep a `Thread` object per finished command. `stop()` skips
388
397
  commands that have not started yet, and so does a Ctrl-C ( or an error ) that interrupts
389
398
  `run_command(s)`, for that call's commands.
@@ -553,6 +562,26 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
553
562
  `scripts/realsudo.py` runs `--sudo` against real sudo (1.8.23, 1.9.5, 1.9.9, 1.9.16) in Docker, with pam_faillock on, and
554
563
  prints a table of what each version of `multiCMD.py` given to it did (`python3 scripts/realsudo.py` says how).
555
564
 
565
+ ## Upgrading to 1.73 from 1.72
566
+
567
+ - Nothing needs doing unless a program builds an `AsyncExecutor` with no `semaphore` argument and a `max_threads` of 0 or
568
+ less, or above 1 that is not whole and not `inf` (`1.5`), or with `semaphore=None`, or gives `max_threads` to its
569
+ `run_command(s)`. `mcmd` and the module-level `run_command(s)` are unchanged.
570
+ - Fix: `AsyncExecutor(max_threads=0)`, or a negative value, with no `semaphore` given, runs every command at once, as
571
+ `run_commands(max_threads=0)` and `mcmd -m 0` do (1.72 started nothing, with no error, and `join()` never returned; a
572
+ negative value raised `ValueError`).
573
+ - Changed: a `max_threads` given to an executor's `run_command(s)` limits that call within the semaphore it uses (the
574
+ executor's, or a `sem` given to the call): it can lower the limit for the call, never raise it, and 0 or less adds none
575
+ (1.72 ignored it there, so `max_threads=1` on an executor of four now runs that call one at a time). It holds back no
576
+ command of another call. A call given a `sem` is limited by that one instead of the executor's, and `sem=None` by its
577
+ `max_threads`, or the executor's when it gives none, as before; with `semaphore=None` on the executor the calls no longer
578
+ wait for each other (1.72 made a call's limit hold up the calls queued after it).
579
+ - Changed: a `max_threads` given to an executor's call, or to its constructor when it makes the semaphore, that is not a
580
+ whole number (`1.5`) raises `TypeError` (1.72 made a semaphore of it that never blocks); below 1 (`0.5` too) and `inf` are
581
+ no limit. A string given to a call raises `TypeError` before anything runs, where 1.72 ignored it under the executor's
582
+ semaphore. `None` given to a call is the same as not giving it (1.72 raised `TypeError` for it on a call with no
583
+ semaphore).
584
+
556
585
  ## Upgrading to 1.72 from 1.71
557
586
 
558
587
  - Nothing needs doing. Runs on a terminal, and runs whose `sudo -v` signs in (NOPASSWD for all, an askpass helper, a
@@ -567,8 +596,8 @@ prints a table of what each version of `multiCMD.py` given to it did (`python3 s
567
596
  specified`). `--dry-run` with no terminal shows plain `sudo`: `sudo -v` does not run in a dry run.
568
597
  - Changed, Windows (off POSIX): no `sudo -v` runs; the commands stay plain `sudo` (1.70 and 1.71 ran a `sudo -v` there and
569
598
  ignored its exit).
570
- - Known, planned for 1.73 ([docs/limitations.md](docs/limitations.md)): `AsyncExecutor(max_threads=0)` starts no command,
571
- and a per-call `max_threads` is ignored while the executor's own semaphore is used (as in 1.70 and 1.71).
599
+ - Known in 1.72, fixed in 1.73 (see above): `AsyncExecutor(max_threads=0)` starts no command, and a per-call
600
+ `max_threads` is ignored while the executor's own semaphore is used (as in 1.70 and 1.71).
572
601
 
573
602
  ## Upgrading to 1.71 from 1.70
574
603
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.72
3
+ Version: 1.73
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -405,10 +405,19 @@ print(ex.get_results())
405
405
  seconds (the constructor's `timeout` is only the per-command inactivity timeout).
406
406
  `AsyncExecutor` takes the same `shell`, `prefix`, `color` and `pty` options as
407
407
  `run_commands`, as constructor defaults or per call. One dispatcher thread
408
- starts each queued command only after taking a permit from the semaphore, so the
409
- executor holds at most one thread per permit plus the dispatcher, however many
410
- commands are queued. A command's `task.thread` is `None` until it starts, and goes
411
- back to `None` once it has finished and the executor has joined its thread, so a
408
+ starts each queued command only after taking a permit from the semaphore, so with a
409
+ limit the executor holds at most one thread per permit plus the dispatcher, however many
410
+ commands are queued. An executor given no semaphore, with `max_threads=0` (or less, or
411
+ `inf`), has no limit, as for `run_commands`. A `max_threads` given to one call limits that
412
+ call within the semaphore it uses (the executor's, or a `sem` given to the call; with
413
+ `sem=None`, the call's own `max_threads`, or the executor's `max_threads` when it gives
414
+ none), never past it, and holds back no command of another call. A call's commands start
415
+ in their order. A `max_threads` below 1 (or `inf`) is no limit; any other value that is
416
+ not a whole number is a `TypeError`, in a call (`None` there means not given) and in a
417
+ constructor that makes the semaphore.
418
+ A command's `task.thread`
419
+ is `None` until it starts, and goes back to `None` once it has finished and the
420
+ executor has joined its thread, so a
412
421
  large batch does not keep a `Thread` object per finished command. `stop()` skips
413
422
  commands that have not started yet, and so does a Ctrl-C ( or an error ) that interrupts
414
423
  `run_command(s)`, for that call's commands.
@@ -578,6 +587,26 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
578
587
  `scripts/realsudo.py` runs `--sudo` against real sudo (1.8.23, 1.9.5, 1.9.9, 1.9.16) in Docker, with pam_faillock on, and
579
588
  prints a table of what each version of `multiCMD.py` given to it did (`python3 scripts/realsudo.py` says how).
580
589
 
590
+ ## Upgrading to 1.73 from 1.72
591
+
592
+ - Nothing needs doing unless a program builds an `AsyncExecutor` with no `semaphore` argument and a `max_threads` of 0 or
593
+ less, or above 1 that is not whole and not `inf` (`1.5`), or with `semaphore=None`, or gives `max_threads` to its
594
+ `run_command(s)`. `mcmd` and the module-level `run_command(s)` are unchanged.
595
+ - Fix: `AsyncExecutor(max_threads=0)`, or a negative value, with no `semaphore` given, runs every command at once, as
596
+ `run_commands(max_threads=0)` and `mcmd -m 0` do (1.72 started nothing, with no error, and `join()` never returned; a
597
+ negative value raised `ValueError`).
598
+ - Changed: a `max_threads` given to an executor's `run_command(s)` limits that call within the semaphore it uses (the
599
+ executor's, or a `sem` given to the call): it can lower the limit for the call, never raise it, and 0 or less adds none
600
+ (1.72 ignored it there, so `max_threads=1` on an executor of four now runs that call one at a time). It holds back no
601
+ command of another call. A call given a `sem` is limited by that one instead of the executor's, and `sem=None` by its
602
+ `max_threads`, or the executor's when it gives none, as before; with `semaphore=None` on the executor the calls no longer
603
+ wait for each other (1.72 made a call's limit hold up the calls queued after it).
604
+ - Changed: a `max_threads` given to an executor's call, or to its constructor when it makes the semaphore, that is not a
605
+ whole number (`1.5`) raises `TypeError` (1.72 made a semaphore of it that never blocks); below 1 (`0.5` too) and `inf` are
606
+ no limit. A string given to a call raises `TypeError` before anything runs, where 1.72 ignored it under the executor's
607
+ semaphore. `None` given to a call is the same as not giving it (1.72 raised `TypeError` for it on a call with no
608
+ semaphore).
609
+
581
610
  ## Upgrading to 1.72 from 1.71
582
611
 
583
612
  - Nothing needs doing. Runs on a terminal, and runs whose `sudo -v` signs in (NOPASSWD for all, an askpass helper, a
@@ -592,8 +621,8 @@ prints a table of what each version of `multiCMD.py` given to it did (`python3 s
592
621
  specified`). `--dry-run` with no terminal shows plain `sudo`: `sudo -v` does not run in a dry run.
593
622
  - Changed, Windows (off POSIX): no `sudo -v` runs; the commands stay plain `sudo` (1.70 and 1.71 ran a `sudo -v` there and
594
623
  ignored its exit).
595
- - Known, planned for 1.73 ([docs/limitations.md](docs/limitations.md)): `AsyncExecutor(max_threads=0)` starts no command,
596
- and a per-call `max_threads` is ignored while the executor's own semaphore is used (as in 1.70 and 1.71).
624
+ - Known in 1.72, fixed in 1.73 (see above): `AsyncExecutor(max_threads=0)` starts no command, and a per-call
625
+ `max_threads` is ignored while the executor's own semaphore is used (as in 1.70 and 1.71).
597
626
 
598
627
  ## Upgrading to 1.71 from 1.70
599
628
 
@@ -13,6 +13,7 @@ import errno
13
13
  import io
14
14
  import itertools
15
15
  import math
16
+ import operator
16
17
  import os
17
18
  import re
18
19
  import select
@@ -29,7 +30,7 @@ import traceback
29
30
  from pprint import pformat
30
31
 
31
32
  #%% global vars
32
- version = '1.72'
33
+ version = '1.73'
33
34
  __version__ = version
34
35
  COMMIT_DATE = '2026-11-28'
35
36
  # fire-and-forget threads (run_commands with wait_for_return=False) that join_threads() waits on. multiCMD takes no
@@ -240,16 +241,22 @@ class CommandError(Exception):
240
241
  class _HeldPermit:
241
242
  '''
242
243
  Semaphore stand-in for a command whose permit the dispatcher already holds ( internal )
243
- Entering is free, exiting releases the permit.
244
+ Entering is free, exiting releases the permit, then lets the executor queue the call's next command ( see
245
+ AsyncExecutor._queue_next ).
244
246
  '''
245
- def __init__(self, semaphore):
247
+ def __init__(self, semaphore, executor, pending):
246
248
  self.semaphore = semaphore
249
+ self.executor = executor
250
+ self.pending = pending
247
251
 
248
252
  def __enter__(self):
249
253
  return self
250
254
 
251
255
  def __exit__(self, *exc_info):
252
- self.semaphore.release()
256
+ try:
257
+ self.semaphore.release()
258
+ finally:
259
+ self.executor._queue_next(self.pending)
253
260
  return False
254
261
 
255
262
  class _Done:
@@ -623,19 +630,33 @@ def _stops(task):
623
630
  '''Whether task's command is to stop, or not start: asked for ( stop ), its call stops ( _unread ), or mcmd's reader quit ( internal )'''
624
631
  return task.stop or task._unread or _reader_quit()
625
632
 
633
+ def _limit(max_threads):
634
+ '''
635
+ How many commands a max_threads lets run at once ( internal ): 0 for no limit, which is any value below 1 and float('inf'),
636
+ else a whole number ( a float that is whole counts, as it did in a semaphore: 2.0 is 2 ). Any other value is a TypeError:
637
+ 1.5, as run_commands() meets it where it counts its workers, and None or a string
638
+ '''
639
+ if max_threads == float('inf'):
640
+ return 0
641
+ if isinstance(max_threads, float) and max_threads.is_integer():
642
+ max_threads = int(max_threads)
643
+ return 0 if max_threads < 1 else operator.index(max_threads)
644
+
626
645
  class AsyncExecutor:
627
646
  def __init__(self, max_threads=1,semaphore=...,timeout=0,quiet=True,dry_run=False,parse=False,shell=False,prefix=False,color=None,pty=False,env=None,extra_env=None,pass_fds=()):
628
647
  '''
629
648
  AsyncExecutor class to run commands in parallel asynchronously
630
649
 
631
650
  Commands are queued and a dispatcher thread starts each one only after taking a permit
632
- from the semaphore, so the executor holds at most one thread per permit it uses plus the
651
+ from the semaphore, so with a limit the executor holds at most one thread per permit it uses plus the
633
652
  dispatcher, however many commands are queued. A command's task.thread is None until it
634
653
  starts, and goes back to None once it has finished and the executor has joined its thread,
635
654
  so finished tasks do not keep their Thread objects.
636
655
  @params:
637
- max_threads: The maximum number of threads to use ( int ) ( Note: if passing semaphore, this likely will be ignored )
638
- semaphore: The semaphore to use for threading ( threading.Semaphore )
656
+ max_threads: The most commands that run at once ( int; 0 or less, or inf: no limit, as for run_commands ). With a semaphore
657
+ given, that semaphore is the limit instead
658
+ semaphore: The semaphore every call's commands take a permit from ( threading.Semaphore; None: none, so a call is
659
+ limited only by its own max_threads )
639
660
  timeout: The inactivity timeout for each command, in seconds ( 0: none ). Not a limit for wait() / join()
640
661
  quiet: Whether to suppress output ( bool )
641
662
  dry_run: Whether to simulate running the commands ( bool )
@@ -651,7 +672,9 @@ class AsyncExecutor:
651
672
  '''
652
673
  self.max_threads = max_threads
653
674
  if semaphore is ...:
654
- semaphore = threading.Semaphore(max_threads)
675
+ # 0 or less is no limit: a semaphore of 0 would start nothing ( and a negative one is a ValueError )
676
+ limit = _limit(max_threads)
677
+ semaphore = threading.Semaphore(limit) if limit else None
655
678
  self.semaphore = semaphore
656
679
  self.tasks = []
657
680
  self.timeout = timeout
@@ -670,7 +693,8 @@ class AsyncExecutor:
670
693
  self.pass_fds = _check_pass_fds(pass_fds)
671
694
  # held while tasks, _queue and _dispatching change together
672
695
  self._lock = threading.Lock()
673
- # (task, sem, timeout, quiet, dry_run, decor, use_pty) waiting for a permit
696
+ # (task, sem, timeout, quiet, dry_run, decor, use_pty, pending) waiting for a permit; pending is the commands of the
697
+ # entry's call that wait for one of its commands to end ( a call with a limit of its own ), None for a call with none
674
698
  self._queue = collections.deque()
675
699
  self._dispatching = False
676
700
  # started threads not yet joined, in start order ( dict used as an ordered set; one dict operation each, no lock )
@@ -738,6 +762,87 @@ class AsyncExecutor:
738
762
  '''
739
763
  self._threads.pop(thread, None)
740
764
 
765
+ def _queue_one(self, pending):
766
+ '''
767
+ Queue the next command of a call's pending ones that is not stopped ( internal ): the stopped ones before it are skipped,
768
+ as the dispatcher skips them, and no dispatcher is started for them
769
+ @params:
770
+ pending: The call's commands that wait for one of its commands to end ( collections.deque )
771
+ @returns:
772
+ bool: Whether one was queued
773
+ '''
774
+ with self._lock:
775
+ while pending:
776
+ entry = pending.popleft()
777
+ if entry[0].stop:
778
+ entry[0]._env = entry[0]._extra_env = None
779
+ else:
780
+ self._queue.append(entry)
781
+ return True
782
+ return False
783
+
784
+ def _queue_next(self, pending):
785
+ '''
786
+ A command of a call with a limit of its own has ended, however it ended: queue the call's next one ( internal )
787
+ A call queues at most as many commands as its limit, and each one that ends queues the next, so the dispatcher waits
788
+ for the semaphore alone, never for a call's limit, and a call held back by its limit holds back no other call.
789
+ @params:
790
+ pending: The call's commands that wait for one of its commands to end ( collections.deque | None: it has none )
791
+ '''
792
+ # ( an empty one stays empty: it only ever gets commands before its call is queued, so most commands end here, unlocked )
793
+ if pending and self._queue_one(pending):
794
+ self._start_dispatcher()
795
+
796
+ def _start_dispatcher(self, refused=()):
797
+ '''
798
+ Start the dispatcher for what is queued, unless one runs ( internal ): the one place that starts one, under the lock, so
799
+ a start that fails leaves no window for a command to queue behind a dispatcher that never ran
800
+ If its thread cannot be made or started, nothing would run what is queued, so that ends here: the commands of refused
801
+ ( the call that asked for the start ) are stopped, a stopped command is skipped, any other fails as one whose own thread
802
+ cannot be started does ( see _dispatch ), and the call of each queues its next, for the next try.
803
+ @params:
804
+ refused: The tasks of the call that asked for the start ( Iterable[Task] )
805
+ @returns:
806
+ BaseException | None: The earliest error of the starts that failed, or None. A Ctrl-C ( or whatever a signal handler
807
+ raises ) that lands as the first start does is returned too ( if that thread runs, it is the dispatcher ); one that
808
+ lands on a retry, after an earlier error, is not: the earlier error is returned
809
+ '''
810
+ first = None
811
+ while True:
812
+ with self._lock:
813
+ if self._dispatching or not self._queue:
814
+ return first
815
+ dispatcher = None
816
+ try:
817
+ dispatcher = _Thread(self._dispatch)
818
+ self._track(dispatcher)
819
+ dispatcher.start()
820
+ except BaseException as e:
821
+ error = e
822
+ if first is None:
823
+ first = e
824
+ if dispatcher is not None:
825
+ if dispatcher._started.is_set():
826
+ self._dispatching = True
827
+ return first
828
+ self._untrack(dispatcher)
829
+ _stop_tasks(refused)
830
+ ended = list(self._queue)
831
+ self._queue.clear()
832
+ else:
833
+ self._dispatching = True
834
+ return first
835
+ for entry in ended:
836
+ task = entry[0]
837
+ if task.stop:
838
+ # never started: it keeps its place in the task list, not its environment
839
+ task._env = task._extra_env = None
840
+ else:
841
+ _fail_task(task, f'cannot start its thread: {error}', error, entry[3])
842
+ for entry in ended:
843
+ if entry[7]:
844
+ self._queue_one(entry[7])
845
+
741
846
  def _dispatch(self):
742
847
  '''
743
848
  Start queued commands one permit at a time ( internal )
@@ -749,7 +854,7 @@ class AsyncExecutor:
749
854
  if not self._queue:
750
855
  self._dispatching = False
751
856
  break
752
- task, sem, timeout, quiet, dry_run, decor, use_pty = self._queue.popleft()
857
+ task, sem, timeout, quiet, dry_run, decor, use_pty, pending = self._queue.popleft()
753
858
  # wait for a permit, but give up on a command that gets stopped meanwhile
754
859
  acquired = False
755
860
  while not task.stop and not acquired:
@@ -759,12 +864,13 @@ class AsyncExecutor:
759
864
  sem.release()
760
865
  # never started: it keeps its place in the task list, not its environment
761
866
  task._env = task._extra_env = None
867
+ self._queue_next(pending)
762
868
  continue
763
869
  # from here the permit is the thread's once it runs, and given back if it never does
764
870
  thread = None
765
871
  try:
766
872
  self._reap()
767
- thread = _Thread(_run_executor_task, (task, _HeldPermit(sem), timeout, quiet, dry_run, self._finished, decor, use_pty), task)
873
+ thread = _Thread(_run_executor_task, (task, _HeldPermit(sem, self, pending), timeout, quiet, dry_run, self._finished, decor, use_pty), task)
768
874
  self._track(thread)
769
875
  thread.start()
770
876
  except Exception as e:
@@ -773,6 +879,7 @@ class AsyncExecutor:
773
879
  sem.release()
774
880
  # it is off the queue and will never run: it failed, as a command multiCMD cannot start does
775
881
  _fail_task(task, f'cannot start its thread: {e}', e, quiet)
882
+ self._queue_next(pending)
776
883
  except BaseException:
777
884
  # let the next run_commands() start a new dispatcher for what is still queued
778
885
  with self._lock:
@@ -787,13 +894,14 @@ class AsyncExecutor:
787
894
  @params:
788
895
  commands: A list of commands to run ( list[str] | list[list[str]] )
789
896
  timeout: The inactivity timeout for each command to override the object default
790
- max_threads: The call's maximum number of threads ( 0 or less: all at once ), to override the object default. It sizes a
791
- semaphore only when the call makes its own ( sem=None, or an executor made with semaphore=None ); with the executor's
792
- own semaphore ( the default ) that one sets the limit, and this only decides whether the call's output counts as concurrent
897
+ max_threads: The most of the call's commands that run at once ( int; 0 or less, inf or None: no limit of its own ), within
898
+ sem's limit: it can lower the executor's limit for the call, never raise it, and holds back no command of another
899
+ call. With no semaphore ( sem=None, or an executor made with semaphore=None or max_threads 0 or less ) the call's
900
+ limit is this, or the object default
793
901
  quiet: Whether to suppress output to override the object default
794
902
  dry_run: Whether to simulate running the commands to override the object default
795
903
  parse: Whether to parse ranged input to override the object default
796
- sem: The semaphore to use for threading to override the object default
904
+ sem: The semaphore the call's commands take a permit from, to override the object default ( None: none )
797
905
  shell: Whether to run commands through the system shell to override the object default
798
906
  prefix: Whether to prefix output lines with [#] to override the object default
799
907
  color: Whether to color output lines to override the object default
@@ -806,7 +914,11 @@ class AsyncExecutor:
806
914
  '''
807
915
  if timeout is ...:
808
916
  timeout = self.timeout
809
- if max_threads is ...:
917
+ # a max_threads given to the call is a limit of the call's own ( None: not given ); without one, the object default is
918
+ # the limit of a call that has no semaphore ( the semaphore it makes is sized to its commands ), and no limit of the
919
+ # call's own beside a semaphore
920
+ own_limit = max_threads is not ... and max_threads is not None
921
+ if not own_limit:
810
922
  max_threads = self.max_threads
811
923
  if quiet is ...:
812
924
  quiet = self.quiet
@@ -830,45 +942,39 @@ class AsyncExecutor:
830
942
  extra_env = self.extra_env
831
943
  if pass_fds is ...:
832
944
  pass_fds = self.pass_fds
833
- # a call that makes its own semaphore and has a max_threads of the wrong type is refused before _build_tasks runs sudo -v
834
- unlimited = not sem and max_threads < 1
945
+ # the call's own limit ( 0: none ): the max_threads it reads, given to it or the object default for a call that has no
946
+ # semaphore. One of the wrong type is refused before _build_tasks runs sudo -v
947
+ limit = _limit(max_threads) if own_limit or not sem else 0
835
948
  taskObjects = _build_tasks(commands, parse=parse, shell=shell, env=env, extra_env=extra_env, pass_fds=pass_fds,
836
949
  dry_run=dry_run)
837
950
  if not sem:
838
- sem = threading.Semaphore(max(len(taskObjects), 1) if unlimited else max_threads)
951
+ # no semaphore: the call's own limit is the only one
952
+ sem = threading.Semaphore(max(len(taskObjects), 1))
953
+ # a call with a limit queues that many of its commands, and the rest as each one ends ( see _queue_next )
954
+ pending = collections.deque() if 0 < limit < len(taskObjects) else None
839
955
  # the executor's task list keeps growing, so commands count as concurrent whenever max_threads allows it
840
956
  concurrent = max_threads != 1
841
957
  use_color = _resolve_color(color, concurrent)
842
958
  use_pty = _resolve_pty(pty)
843
959
  self._reap()
844
- start_dispatcher = False
845
- dispatcher = None
846
960
  try:
847
961
  with self._lock:
848
962
  first = len(self.tasks) + 1
849
- queued = [(task, sem, timeout, quiet, dry_run, _decor(first + i, 0, prefix and concurrent, use_color), use_pty)
963
+ queued = [(task, sem, timeout, quiet, dry_run, _decor(first + i, 0, prefix and concurrent, use_color), use_pty, pending)
850
964
  for i, task in enumerate(taskObjects)]
851
965
  self.tasks.extend(taskObjects)
966
+ if pending is not None:
967
+ pending.extend(queued[limit:])
968
+ del queued[limit:]
852
969
  self._queue.extend(queued)
853
- if not self._dispatching and self._queue:
854
- start_dispatcher = True
855
- # only the dispatcher clears this, under the lock, once it sees the queue empty ( or the handler below,
856
- # when none started )
857
- self._dispatching = True
858
- if start_dispatcher:
859
- dispatcher = _Thread(self._dispatch)
860
- self._track(dispatcher)
861
- dispatcher.start()
970
+ error = self._start_dispatcher(taskObjects)
971
+ if error is not None:
972
+ raise error
862
973
  except BaseException:
863
974
  # a Ctrl-C or a failure from here on: this call's commands are skipped, whoever would start them; a
864
975
  # dispatcher that started stays the dispatcher, and without one the next run_commands() starts one for what
865
- # is still queued ( one store, without the lock: a run_commands() from another thread that saw it still set
866
- # leaves its commands queued for the next one, as it would have had it taken the lock first )
976
+ # is still queued ( the commands of other calls that were queued with none are ended by _start_dispatcher )
867
977
  _stop_tasks(taskObjects)
868
- if start_dispatcher and (dispatcher is None or not dispatcher._started.is_set()):
869
- if dispatcher is not None:
870
- self._untrack(dispatcher)
871
- self._dispatching = False
872
978
  raise
873
979
  return taskObjects
874
980
 
@@ -2844,7 +2950,7 @@ def run_commands(commands, timeout=0,max_threads=1,quiet=False,dry_run=False,wit
2844
2950
  return_object: Whether to return the Task object
2845
2951
  parse: Whether to parse ranged input
2846
2952
  wait_for_return: Whether to wait for the return of the commands
2847
- sem: The semaphore to use for threading
2953
+ sem: The semaphore to use for threading ( with wait_for_return=False the semaphore alone limits the commands: max_threads is not applied )
2848
2954
  use_sudo: Whether to use sudo for commands. On a terminal each runs as sudo -n, so one that sudo would ask a password
2849
2955
  for fails at once ( 'sudo: a password is required' ); sudo -v asks for it first, in the terminal's foreground only.
2850
2956
  With no terminal each runs as plain sudo where sudo -v signed in, as sudo -n where it failed ( off POSIX: plain sudo, no sudo -v )