[{"data":1,"prerenderedAt":2533},["ShallowReactive",2],{"page-\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002F":3,"content-directory":1986},{"id":4,"title":5,"body":6,"date":1971,"description":1972,"difficulty":1973,"draft":1974,"extension":1975,"meta":1976,"navigation":192,"path":1977,"seo":1978,"stem":1979,"tags":1980,"updated":1971,"__hash__":1985},"content\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Findex.md","Long-Running and Watch-Mode Python CLIs",{"type":7,"value":8,"toc":1949},"minimark",[9,26,50,54,59,109,113,116,119,141,145,148,151,796,799,833,837,856,859,896,900,917,1173,1194,1198,1209,1212,1257,1265,1269,1276,1303,1310,1314,1317,1328,1334,1353,1359,1363,1374,1406,1414,1418,1421,1447,1801,1805,1831,1835,1840,1847,1851,1858,1862,1869,1873,1880,1884,1897,1901,1904,1908,1945],[10,11,12,13,17,18,21,22,25],"p",{},"Most CLI commands run for a second and exit. Some do not. A ",[14,15,16],"code",{},"build --watch"," that rebuilds every time a file changes. A ",[14,19,20],{},"sync"," command that runs every night from cron. A queue consumer or log shipper packaged as a CLI subcommand and run under systemd, Docker or Kubernetes. A ",[14,23,24],{},"tail","-like command that follows an event stream until someone stops it. These long-running commands face problems short ones never meet: they are stopped by signals rather than by finishing, they run without a person watching, they are started by machines with minimal environments, and nobody notices when they quietly stop doing their job.",[10,27,28,29,34,35,39,40,44,45,49],{},"This topic covers building those commands properly in Python: shutting down gracefully when asked, watching files without rebuilding in loops, running reliably on a schedule, and exposing health so that something notices when they stall. It completes the ",[30,31,33],"a",{"href":32},"\u002Fcli-runtime-systems-integration\u002F","CLI Runtime & Systems Integration"," section, and it leans on the others — ",[30,36,38],{"href":37},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Ffile-locking-for-concurrent-cli-runs\u002F","file locking"," to prevent overlapping runs, ",[30,41,43],{"href":42},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Fcancelling-async-tasks-on-ctrl-c\u002F","cancellation"," for async loops, and ",[30,46,48],{"href":47},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Freading-secrets-from-env-and-files\u002F","secrets from the environment"," for unattended runs.",[51,52],"inline-diagram",{"name":53},"lr-topic-map",[55,56,58],"h2",{"id":57},"tldr","TL;DR",[60,61,62,81,87,93,99],"ul",{},[63,64,65,73,74,76,77,80],"li",{},[66,67,68,69,72],"strong",{},"Handle ",[14,70,71],{},"SIGTERM"," like Ctrl+C."," Supervisors stop you with ",[14,75,71],{},", and Python's default is to die without running ",[14,78,79],{},"finally"," blocks. Install a handler that sets a stop event.",[63,82,83,86],{},[66,84,85],{},"Structure the loop around one bounded unit of work",", and make every wait wake up on the stop event.",[63,88,89,92],{},[66,90,91],{},"Debounce and filter file events"," in watch mode, ignore your own outputs, and keep watching after a failed rebuild.",[63,94,95,98],{},[66,96,97],{},"Make scheduled commands non-interactive, idempotent and overlap-safe",", with absolute paths and meaningful exit codes. Prefer systemd timers where available.",[63,100,101,104,105,108],{},[66,102,103],{},"Emit a heartbeat"," after each unit of work and provide a ",[14,106,107],{},"health"," command so probes and monitors can tell \"running\" from \"working\".",[55,110,112],{"id":111},"three-shapes-of-long-running-command","Three shapes of long-running command",[10,114,115],{},"It helps to recognise which shape you are building, because each has a different priority:",[51,117],{"name":118},"lr-shapes-matrix",[10,120,121,122,125,126,129,130,133,134,136,137,140],{},"A ",[66,123,124],{},"watch loop"," is started and stopped by a developer at a terminal; responsiveness and readable output matter most. A ",[66,127,128],{},"supervised service"," is started and stopped by systemd, a container runtime or Kubernetes, which communicate through signals and expect a timely exit. A ",[66,131,132],{},"scheduled job"," runs to completion on a timer, unattended; it must be safe to repeat, must never overlap with itself, and must fail loudly enough that someone notices. Many real tools are all three at once — the same ",[14,135,20],{}," command is run by hand, in ",[14,138,139],{},"--watch"," mode during development and nightly from a timer — so it is worth designing the core loop once, correctly.",[55,142,144],{"id":143},"the-shape-of-a-good-loop","The shape of a good loop",[10,146,147],{},"Almost every long-running command is a loop: wait for something to do, do it, repeat. The details of that loop determine how the command behaves when it is stopped, when it fails and when it stalls.",[51,149],{"name":150},"lr-main-loop",[152,153,158],"pre",{"className":154,"code":155,"language":156,"meta":157,"style":157},"language-python shiki shiki-themes github-light github-dark","# src\u002Fmytool\u002Floop.py\nfrom __future__ import annotations\n\nimport logging\nimport signal\nimport threading\nimport time\nfrom collections.abc import Callable\nfrom pathlib import Path\n\nlog = logging.getLogger(__name__)\n\n\nclass Stopper:\n    \"\"\"Turns SIGINT\u002FSIGTERM into a stop event the loop can wait on.\"\"\"\n\n    def __init__(self) -> None:\n        self.event = threading.Event()\n        self.signum: int | None = None\n\n    def install(self) -> None:\n        for sig in (signal.SIGINT, signal.SIGTERM):\n            signal.signal(sig, self._handle)\n\n    def _handle(self, signum: int, frame: object) -> None:\n        if self.event.is_set():                    # second signal: stop waiting politely\n            raise KeyboardInterrupt\n        self.signum = signum\n        self.event.set()\n\n    @property\n    def exit_code(self) -> int:\n        return 128 + self.signum if self.signum else 0\n\n\ndef run_forever(unit: Callable[[], int], *, interval: float, stopper: Stopper,\n                heartbeat: Path | None = None) -> int:\n    \"\"\"Call `unit` every `interval` seconds until stopped; return the exit code.\"\"\"\n    while not stopper.event.is_set():\n        started = time.monotonic()\n        try:\n            done = unit()\n            log.info(\"processed %d items\", done)\n        except Exception:\n            log.exception(\"unit of work failed; will retry next interval\")\n        if heartbeat is not None:\n            heartbeat.touch()\n        remaining = interval - (time.monotonic() - started)\n        stopper.event.wait(max(0.0, remaining))    # wakes immediately on a signal\n    log.info(\"stopping after signal %s\", stopper.signum)\n    return stopper.exit_code\n","python","",[14,159,160,169,187,194,203,211,219,227,240,253,258,276,281,286,299,306,311,328,342,365,370,384,410,422,427,453,468,477,490,498,503,512,526,555,560,565,594,615,621,633,644,652,663,681,692,703,720,726,748,769,787],{"__ignoreMap":157},[161,162,165],"span",{"class":163,"line":164},"line",1,[161,166,168],{"class":167},"sJ8bj","# src\u002Fmytool\u002Floop.py\n",[161,170,172,176,180,183],{"class":163,"line":171},2,[161,173,175],{"class":174},"szBVR","from",[161,177,179],{"class":178},"sj4cs"," __future__",[161,181,182],{"class":174}," import",[161,184,186],{"class":185},"sVt8B"," annotations\n",[161,188,190],{"class":163,"line":189},3,[161,191,193],{"emptyLinePlaceholder":192},true,"\n",[161,195,197,200],{"class":163,"line":196},4,[161,198,199],{"class":174},"import",[161,201,202],{"class":185}," logging\n",[161,204,206,208],{"class":163,"line":205},5,[161,207,199],{"class":174},[161,209,210],{"class":185}," signal\n",[161,212,214,216],{"class":163,"line":213},6,[161,215,199],{"class":174},[161,217,218],{"class":185}," threading\n",[161,220,222,224],{"class":163,"line":221},7,[161,223,199],{"class":174},[161,225,226],{"class":185}," time\n",[161,228,230,232,235,237],{"class":163,"line":229},8,[161,231,175],{"class":174},[161,233,234],{"class":185}," collections.abc ",[161,236,199],{"class":174},[161,238,239],{"class":185}," Callable\n",[161,241,243,245,248,250],{"class":163,"line":242},9,[161,244,175],{"class":174},[161,246,247],{"class":185}," pathlib ",[161,249,199],{"class":174},[161,251,252],{"class":185}," Path\n",[161,254,256],{"class":163,"line":255},10,[161,257,193],{"emptyLinePlaceholder":192},[161,259,261,264,267,270,273],{"class":163,"line":260},11,[161,262,263],{"class":185},"log ",[161,265,266],{"class":174},"=",[161,268,269],{"class":185}," logging.getLogger(",[161,271,272],{"class":178},"__name__",[161,274,275],{"class":185},")\n",[161,277,279],{"class":163,"line":278},12,[161,280,193],{"emptyLinePlaceholder":192},[161,282,284],{"class":163,"line":283},13,[161,285,193],{"emptyLinePlaceholder":192},[161,287,289,292,296],{"class":163,"line":288},14,[161,290,291],{"class":174},"class",[161,293,295],{"class":294},"sScJk"," Stopper",[161,297,298],{"class":185},":\n",[161,300,302],{"class":163,"line":301},15,[161,303,305],{"class":304},"sZZnC","    \"\"\"Turns SIGINT\u002FSIGTERM into a stop event the loop can wait on.\"\"\"\n",[161,307,309],{"class":163,"line":308},16,[161,310,193],{"emptyLinePlaceholder":192},[161,312,314,317,320,323,326],{"class":163,"line":313},17,[161,315,316],{"class":174},"    def",[161,318,319],{"class":178}," __init__",[161,321,322],{"class":185},"(self) -> ",[161,324,325],{"class":178},"None",[161,327,298],{"class":185},[161,329,331,334,337,339],{"class":163,"line":330},18,[161,332,333],{"class":178},"        self",[161,335,336],{"class":185},".event ",[161,338,266],{"class":174},[161,340,341],{"class":185}," threading.Event()\n",[161,343,345,347,350,353,356,359,362],{"class":163,"line":344},19,[161,346,333],{"class":178},[161,348,349],{"class":185},".signum: ",[161,351,352],{"class":178},"int",[161,354,355],{"class":174}," |",[161,357,358],{"class":178}," None",[161,360,361],{"class":174}," =",[161,363,364],{"class":178}," None\n",[161,366,368],{"class":163,"line":367},20,[161,369,193],{"emptyLinePlaceholder":192},[161,371,373,375,378,380,382],{"class":163,"line":372},21,[161,374,316],{"class":174},[161,376,377],{"class":294}," install",[161,379,322],{"class":185},[161,381,325],{"class":178},[161,383,298],{"class":185},[161,385,387,390,393,396,399,402,405,407],{"class":163,"line":386},22,[161,388,389],{"class":174},"        for",[161,391,392],{"class":185}," sig ",[161,394,395],{"class":174},"in",[161,397,398],{"class":185}," (signal.",[161,400,401],{"class":178},"SIGINT",[161,403,404],{"class":185},", signal.",[161,406,71],{"class":178},[161,408,409],{"class":185},"):\n",[161,411,413,416,419],{"class":163,"line":412},23,[161,414,415],{"class":185},"            signal.signal(sig, ",[161,417,418],{"class":178},"self",[161,420,421],{"class":185},"._handle)\n",[161,423,425],{"class":163,"line":424},24,[161,426,193],{"emptyLinePlaceholder":192},[161,428,430,432,435,438,440,443,446,449,451],{"class":163,"line":429},25,[161,431,316],{"class":174},[161,433,434],{"class":294}," _handle",[161,436,437],{"class":185},"(self, signum: ",[161,439,352],{"class":178},[161,441,442],{"class":185},", frame: ",[161,444,445],{"class":178},"object",[161,447,448],{"class":185},") -> ",[161,450,325],{"class":178},[161,452,298],{"class":185},[161,454,456,459,462,465],{"class":163,"line":455},26,[161,457,458],{"class":174},"        if",[161,460,461],{"class":178}," self",[161,463,464],{"class":185},".event.is_set():                    ",[161,466,467],{"class":167},"# second signal: stop waiting politely\n",[161,469,471,474],{"class":163,"line":470},27,[161,472,473],{"class":174},"            raise",[161,475,476],{"class":178}," KeyboardInterrupt\n",[161,478,480,482,485,487],{"class":163,"line":479},28,[161,481,333],{"class":178},[161,483,484],{"class":185},".signum ",[161,486,266],{"class":174},[161,488,489],{"class":185}," signum\n",[161,491,493,495],{"class":163,"line":492},29,[161,494,333],{"class":178},[161,496,497],{"class":185},".event.set()\n",[161,499,501],{"class":163,"line":500},30,[161,502,193],{"emptyLinePlaceholder":192},[161,504,506,509],{"class":163,"line":505},31,[161,507,508],{"class":294},"    @",[161,510,511],{"class":178},"property\n",[161,513,515,517,520,522,524],{"class":163,"line":514},32,[161,516,316],{"class":174},[161,518,519],{"class":294}," exit_code",[161,521,322],{"class":185},[161,523,352],{"class":178},[161,525,298],{"class":185},[161,527,529,532,535,538,540,542,545,547,549,552],{"class":163,"line":528},33,[161,530,531],{"class":174},"        return",[161,533,534],{"class":178}," 128",[161,536,537],{"class":174}," +",[161,539,461],{"class":178},[161,541,484],{"class":185},[161,543,544],{"class":174},"if",[161,546,461],{"class":178},[161,548,484],{"class":185},[161,550,551],{"class":174},"else",[161,553,554],{"class":178}," 0\n",[161,556,558],{"class":163,"line":557},34,[161,559,193],{"emptyLinePlaceholder":192},[161,561,563],{"class":163,"line":562},35,[161,564,193],{"emptyLinePlaceholder":192},[161,566,568,571,574,577,579,582,585,588,591],{"class":163,"line":567},36,[161,569,570],{"class":174},"def",[161,572,573],{"class":294}," run_forever",[161,575,576],{"class":185},"(unit: Callable[[], ",[161,578,352],{"class":178},[161,580,581],{"class":185},"], ",[161,583,584],{"class":174},"*",[161,586,587],{"class":185},", interval: ",[161,589,590],{"class":178},"float",[161,592,593],{"class":185},", stopper: Stopper,\n",[161,595,597,600,603,605,607,609,611,613],{"class":163,"line":596},37,[161,598,599],{"class":185},"                heartbeat: Path ",[161,601,602],{"class":174},"|",[161,604,358],{"class":178},[161,606,361],{"class":174},[161,608,358],{"class":178},[161,610,448],{"class":185},[161,612,352],{"class":178},[161,614,298],{"class":185},[161,616,618],{"class":163,"line":617},38,[161,619,620],{"class":304},"    \"\"\"Call `unit` every `interval` seconds until stopped; return the exit code.\"\"\"\n",[161,622,624,627,630],{"class":163,"line":623},39,[161,625,626],{"class":174},"    while",[161,628,629],{"class":174}," not",[161,631,632],{"class":185}," stopper.event.is_set():\n",[161,634,636,639,641],{"class":163,"line":635},40,[161,637,638],{"class":185},"        started ",[161,640,266],{"class":174},[161,642,643],{"class":185}," time.monotonic()\n",[161,645,647,650],{"class":163,"line":646},41,[161,648,649],{"class":174},"        try",[161,651,298],{"class":185},[161,653,655,658,660],{"class":163,"line":654},42,[161,656,657],{"class":185},"            done ",[161,659,266],{"class":174},[161,661,662],{"class":185}," unit()\n",[161,664,666,669,672,675,678],{"class":163,"line":665},43,[161,667,668],{"class":185},"            log.info(",[161,670,671],{"class":304},"\"processed ",[161,673,674],{"class":178},"%d",[161,676,677],{"class":304}," items\"",[161,679,680],{"class":185},", done)\n",[161,682,684,687,690],{"class":163,"line":683},44,[161,685,686],{"class":174},"        except",[161,688,689],{"class":178}," Exception",[161,691,298],{"class":185},[161,693,695,698,701],{"class":163,"line":694},45,[161,696,697],{"class":185},"            log.exception(",[161,699,700],{"class":304},"\"unit of work failed; will retry next interval\"",[161,702,275],{"class":185},[161,704,706,708,711,714,716,718],{"class":163,"line":705},46,[161,707,458],{"class":174},[161,709,710],{"class":185}," heartbeat ",[161,712,713],{"class":174},"is",[161,715,629],{"class":174},[161,717,358],{"class":178},[161,719,298],{"class":185},[161,721,723],{"class":163,"line":722},47,[161,724,725],{"class":185},"            heartbeat.touch()\n",[161,727,729,732,734,737,740,743,745],{"class":163,"line":728},48,[161,730,731],{"class":185},"        remaining ",[161,733,266],{"class":174},[161,735,736],{"class":185}," interval ",[161,738,739],{"class":174},"-",[161,741,742],{"class":185}," (time.monotonic() ",[161,744,739],{"class":174},[161,746,747],{"class":185}," started)\n",[161,749,751,754,757,760,763,766],{"class":163,"line":750},49,[161,752,753],{"class":185},"        stopper.event.wait(",[161,755,756],{"class":178},"max",[161,758,759],{"class":185},"(",[161,761,762],{"class":178},"0.0",[161,764,765],{"class":185},", remaining))    ",[161,767,768],{"class":167},"# wakes immediately on a signal\n",[161,770,772,775,778,781,784],{"class":163,"line":771},50,[161,773,774],{"class":185},"    log.info(",[161,776,777],{"class":304},"\"stopping after signal ",[161,779,780],{"class":178},"%s",[161,782,783],{"class":304},"\"",[161,785,786],{"class":185},", stopper.signum)\n",[161,788,790,793],{"class":163,"line":789},51,[161,791,792],{"class":174},"    return",[161,794,795],{"class":185}," stopper.exit_code\n",[10,797,798],{},"Four properties make this loop well-behaved:",[60,800,801,815,821,827],{},[63,802,803,806,807,810,811,814],{},[66,804,805],{},"The wait is interruptible."," ",[14,808,809],{},"event.wait(timeout)"," returns the moment the stop event is set, so a command with a five-minute interval still stops within milliseconds. ",[14,812,813],{},"time.sleep(300)"," would make shutdown wait for the rest of the interval — or be killed by the supervisor first.",[63,816,817,820],{},[66,818,819],{},"Units of work are bounded."," A signal is only noticed between units. If a unit can take ten minutes, the loop cannot honour a thirty-second grace period; split the work, or check the stop event inside it.",[63,822,823,826],{},[66,824,825],{},"One failure does not end the loop."," An unexpected error is logged with its traceback and the loop carries on at the next interval — the right default for a service. For a scheduled one-shot job, the opposite is right: fail and exit non-zero.",[63,828,829,832],{},[66,830,831],{},"Progress is visible."," A heartbeat file is touched after every unit, which is what health checks read.",[55,834,836],{"id":835},"being-stopped-signals","Being stopped: signals",[10,838,839,840,842,843,846,847,849,850,852,853,855],{},"A command at a terminal is stopped with Ctrl+C, which sends ",[14,841,401],{}," and, by default, raises ",[14,844,845],{},"KeyboardInterrupt",". A command under a supervisor is stopped with ",[14,848,71],{},", and Python's default action for ",[14,851,71],{}," is to terminate immediately — no exception, no ",[14,854,79],{}," blocks, no flushing of buffered output, no removal of lock files or temporary directories.",[51,857],{"name":858},"lr-stop-sources",[10,860,861,862,865,866,869,870,872,873,875,876,878,879,882,883,886,887,890,891,895],{},"The ",[14,863,864],{},"Stopper"," above treats both signals the same way: it sets an event, the loop finishes its current unit and returns, and the command exits with ",[14,867,868],{},"128 + signal"," — 130 for ",[14,871,401],{},", 143 for ",[14,874,71],{}," — the codes supervisors and shells expect. A second signal while shutting down raises ",[14,877,845],{}," to abandon slow cleanup, the same escape hatch ",[14,880,881],{},"asyncio.run"," provides. Supervisors escalate to ",[14,884,885],{},"SIGKILL"," after a grace period — 10 seconds for ",[14,888,889],{},"docker stop",", 30 for Kubernetes, 90 for systemd by default — so cleanup must fit well inside the shortest. ",[30,892,894],{"href":893},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fhandling-sigterm-and-graceful-shutdown\u002F","Handling SIGTERM and graceful shutdown"," covers the details, including containers where your CLI runs as PID 1.",[55,897,899],{"id":898},"watch-mode","Watch mode",[10,901,902,904,905,908,909,912,913,916],{},[14,903,139],{}," is one of the most-loved features a developer tool can have: save a file and see the result a fraction of a second later. Implementing it with a polling loop over ",[14,906,907],{},"os.stat"," is slow and CPU-hungry; implementing it naively with OS file events leads to rebuilding five times per save, or rebuilding forever because each build writes files the watcher then notices. The ",[14,910,911],{},"watchfiles"," package, built on Rust's ",[14,914,915],{},"notify"," library, handles the platform-specific event APIs, debounces bursts of events into batches, and supports filters:",[152,918,920],{"className":154,"code":919,"language":156,"meta":157,"style":157},"from pathlib import Path\n\nfrom watchfiles import DefaultFilter, watch\n\n\nclass SourceFilter(DefaultFilter):\n    def __init__(self, output_dir: Path) -> None:\n        super().__init__()\n        self.output_dir = output_dir.resolve()\n\n    def __call__(self, change, path: str) -> bool:\n        return super().__call__(change, path) and not Path(path).resolve().is_relative_to(self.output_dir)\n\n\ndef watch_and_build(src: Path, out: Path, build) -> None:\n    build()\n    for changes in watch(src, watch_filter=SourceFilter(out), debounce=200):\n        try:\n            build()\n        except Exception as exc:          # a broken file must not end the watch\n            print(f\"error: {exc} — still watching\")\n",[14,921,922,932,936,948,952,956,970,983,997,1009,1013,1033,1061,1065,1069,1083,1088,1120,1126,1131,1146],{"__ignoreMap":157},[161,923,924,926,928,930],{"class":163,"line":164},[161,925,175],{"class":174},[161,927,247],{"class":185},[161,929,199],{"class":174},[161,931,252],{"class":185},[161,933,934],{"class":163,"line":171},[161,935,193],{"emptyLinePlaceholder":192},[161,937,938,940,943,945],{"class":163,"line":189},[161,939,175],{"class":174},[161,941,942],{"class":185}," watchfiles ",[161,944,199],{"class":174},[161,946,947],{"class":185}," DefaultFilter, watch\n",[161,949,950],{"class":163,"line":196},[161,951,193],{"emptyLinePlaceholder":192},[161,953,954],{"class":163,"line":205},[161,955,193],{"emptyLinePlaceholder":192},[161,957,958,960,963,965,968],{"class":163,"line":213},[161,959,291],{"class":174},[161,961,962],{"class":294}," SourceFilter",[161,964,759],{"class":185},[161,966,967],{"class":294},"DefaultFilter",[161,969,409],{"class":185},[161,971,972,974,976,979,981],{"class":163,"line":221},[161,973,316],{"class":174},[161,975,319],{"class":178},[161,977,978],{"class":185},"(self, output_dir: Path) -> ",[161,980,325],{"class":178},[161,982,298],{"class":185},[161,984,985,988,991,994],{"class":163,"line":229},[161,986,987],{"class":178},"        super",[161,989,990],{"class":185},"().",[161,992,993],{"class":178},"__init__",[161,995,996],{"class":185},"()\n",[161,998,999,1001,1004,1006],{"class":163,"line":242},[161,1000,333],{"class":178},[161,1002,1003],{"class":185},".output_dir ",[161,1005,266],{"class":174},[161,1007,1008],{"class":185}," output_dir.resolve()\n",[161,1010,1011],{"class":163,"line":255},[161,1012,193],{"emptyLinePlaceholder":192},[161,1014,1015,1017,1020,1023,1026,1028,1031],{"class":163,"line":260},[161,1016,316],{"class":174},[161,1018,1019],{"class":178}," __call__",[161,1021,1022],{"class":185},"(self, change, path: ",[161,1024,1025],{"class":178},"str",[161,1027,448],{"class":185},[161,1029,1030],{"class":178},"bool",[161,1032,298],{"class":185},[161,1034,1035,1037,1040,1042,1045,1048,1051,1053,1056,1058],{"class":163,"line":278},[161,1036,531],{"class":174},[161,1038,1039],{"class":178}," super",[161,1041,990],{"class":185},[161,1043,1044],{"class":178},"__call__",[161,1046,1047],{"class":185},"(change, path) ",[161,1049,1050],{"class":174},"and",[161,1052,629],{"class":174},[161,1054,1055],{"class":185}," Path(path).resolve().is_relative_to(",[161,1057,418],{"class":178},[161,1059,1060],{"class":185},".output_dir)\n",[161,1062,1063],{"class":163,"line":283},[161,1064,193],{"emptyLinePlaceholder":192},[161,1066,1067],{"class":163,"line":288},[161,1068,193],{"emptyLinePlaceholder":192},[161,1070,1071,1073,1076,1079,1081],{"class":163,"line":301},[161,1072,570],{"class":174},[161,1074,1075],{"class":294}," watch_and_build",[161,1077,1078],{"class":185},"(src: Path, out: Path, build) -> ",[161,1080,325],{"class":178},[161,1082,298],{"class":185},[161,1084,1085],{"class":163,"line":308},[161,1086,1087],{"class":185},"    build()\n",[161,1089,1090,1093,1096,1098,1101,1105,1107,1110,1113,1115,1118],{"class":163,"line":313},[161,1091,1092],{"class":174},"    for",[161,1094,1095],{"class":185}," changes ",[161,1097,395],{"class":174},[161,1099,1100],{"class":185}," watch(src, ",[161,1102,1104],{"class":1103},"s4XuR","watch_filter",[161,1106,266],{"class":174},[161,1108,1109],{"class":185},"SourceFilter(out), ",[161,1111,1112],{"class":1103},"debounce",[161,1114,266],{"class":174},[161,1116,1117],{"class":178},"200",[161,1119,409],{"class":185},[161,1121,1122,1124],{"class":163,"line":330},[161,1123,649],{"class":174},[161,1125,298],{"class":185},[161,1127,1128],{"class":163,"line":344},[161,1129,1130],{"class":185},"            build()\n",[161,1132,1133,1135,1137,1140,1143],{"class":163,"line":367},[161,1134,686],{"class":174},[161,1136,689],{"class":178},[161,1138,1139],{"class":174}," as",[161,1141,1142],{"class":185}," exc:          ",[161,1144,1145],{"class":167},"# a broken file must not end the watch\n",[161,1147,1148,1151,1153,1156,1159,1162,1165,1168,1171],{"class":163,"line":372},[161,1149,1150],{"class":178},"            print",[161,1152,759],{"class":185},[161,1154,1155],{"class":174},"f",[161,1157,1158],{"class":304},"\"error: ",[161,1160,1161],{"class":178},"{",[161,1163,1164],{"class":185},"exc",[161,1166,1167],{"class":178},"}",[161,1169,1170],{"class":304}," — still watching\"",[161,1172,275],{"class":185},[10,1174,1175,1177,1178,1181,1182,1185,1186,1190,1191,1193],{},[14,1176,967],{}," already ignores ",[14,1179,1180],{},".git",", ",[14,1183,1184],{},"__pycache__",", virtual environments and common editor swap files; the subclass adds your own output directory, which prevents the infinite rebuild loop. ",[30,1187,1189],{"href":1188},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fbuilding-a-watch-mode-with-watchfiles\u002F","Building a watch mode with watchfiles"," develops this into a complete ",[14,1192,139],{}," flag with timestamped output, incremental rebuilds and tests.",[55,1195,1197],{"id":1196},"running-on-a-schedule","Running on a schedule",[10,1199,1200,1201,1204,1205,1208],{},"The most common long-running arrangement is not a process that runs forever but one that runs ",[66,1202,1203],{},"again and again",": a nightly sync, an hourly report, a cleanup every Sunday. The scheduler — cron, a systemd timer, a CI schedule, Kubernetes CronJobs — starts your command in an environment that is nothing like your interactive shell: a minimal ",[14,1206,1207],{},"PATH",", no shell profile, no terminal, a different working directory, often a different user. Commands that work perfectly by hand fail there in predictable ways.",[10,1210,1211],{},"Before scheduling a command, it should be:",[60,1213,1214,1225,1231,1245,1251],{},[63,1215,1216,1219,1220,1224],{},[66,1217,1218],{},"Non-interactive",": never prompt; detect the missing terminal and fail with a message instead, as in ",[30,1221,1223],{"href":1222},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fprompting-for-passwords-securely\u002F","prompting for passwords securely",".",[63,1226,1227,1230],{},[66,1228,1229],{},"Idempotent",": safe to run twice with the same result, because schedulers retry and humans rerun.",[63,1232,1233,1236,1237,1240,1241,1244],{},[66,1234,1235],{},"Overlap-safe",": if a run takes longer than the interval, the next one must not race it — a ",[30,1238,1239],{"href":37},"lock"," with ",[14,1242,1243],{},"--no-wait"," is the usual answer.",[63,1246,1247,1250],{},[66,1248,1249],{},"Explicit about paths",": absolute paths or paths from config, never \"the current directory\".",[63,1252,1253,1256],{},[66,1254,1255],{},"Honest in its exit code",": non-zero on any failure, so the scheduler, the journal or an alerting wrapper can notice.",[10,1258,1259,1260,1264],{},"systemd timers add journald logging, catch-up of missed runs and built-in protection against overlap, and are preferable to cron where available. ",[30,1261,1263],{"href":1262},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Frunning-a-cli-on-a-schedule-with-cron-and-systemd\u002F","Running a CLI on a schedule with cron and systemd"," has complete, copy-pasteable configurations for both.",[55,1266,1268],{"id":1267},"knowing-it-still-works","Knowing it still works",[10,1270,1271,1272,1275],{},"A long-running process that is alive but stuck — waiting forever on a hung connection, spinning on an error — looks healthy to everything that only checks whether the process exists. A scheduled job that silently stopped being scheduled looks like nothing at all. Both need a signal of ",[66,1273,1274],{},"progress",", not mere existence:",[60,1277,1278,1292],{},[63,1279,121,1280,1283,1284,1287,1288,1291],{},[66,1281,1282],{},"heartbeat"," — a file touched, a timestamp written or a metric updated after every unit of work — shows the loop is making progress. A ",[14,1285,1286],{},"mytool health --max-age 120"," command that checks the heartbeat's age gives Docker ",[14,1289,1290],{},"HEALTHCHECK",", Kubernetes liveness probes and monitoring scripts a simple exit-code API.",[63,1293,121,1294,1297,1298,1302],{},[66,1295,1296],{},"dead-man switch"," — an external service pinged at the end of each successful scheduled run, which alerts when a ping does ",[1299,1300,1301],"em",{},"not"," arrive — catches the failure no in-process check can: the job never running at all.",[10,1304,1305,1309],{},[30,1306,1308],{"href":1307},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fhealth-checks-and-heartbeats-for-long-running-clis\u002F","Health checks and heartbeats for long-running CLIs"," implements both.",[55,1311,1313],{"id":1312},"crashes-restarts-and-picking-up-where-you-left-off","Crashes, restarts and picking up where you left off",[10,1315,1316],{},"A long-running command will be restarted: after a crash, after a deploy, after the machine reboots, after the supervisor decides it is unhealthy. Whether that restart is harmless or costly depends on decisions made in the loop long before anything goes wrong.",[10,1318,1319,1322,1323,1327],{},[66,1320,1321],{},"Record progress after each unit, not at the end."," A command that processes 10,000 records and writes its checkpoint only when it finishes loses everything on a crash at record 9,000. Write the checkpoint — the last processed ID, cursor or timestamp — after each unit, atomically, using the pattern from ",[30,1324,1326],{"href":1325},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fwriting-files-atomically-in-python-clis\u002F","writing files atomically in Python CLIs",". A restart then resumes from the last completed unit.",[10,1329,1330,1333],{},[66,1331,1332],{},"Make units idempotent."," A crash between doing the work and recording the checkpoint means the unit will be done again. If \"done again\" means \"a second email sent\" or \"the same payment charged twice\", you need idempotency keys or a check before acting; if it means \"the same file uploaded again\", it is merely wasteful.",[10,1335,1336,1339,1340,1343,1344,1347,1348,1352],{},[66,1337,1338],{},"Back off after repeated failures."," A command that crashes on start because a dependency is down will be restarted immediately by most supervisors, crash again, and spin. systemd's ",[14,1341,1342],{},"RestartSec="," and ",[14,1345,1346],{},"StartLimitBurst="," or Kubernetes' crash-loop back-off handle this outside the process; inside it, apply the same capped exponential backoff described in ",[30,1349,1351],{"href":1350},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fretries-and-backoff-for-cli-http-calls\u002F","retries and backoff for CLI HTTP calls"," to the loop's own retry of a failing unit.",[10,1354,1355,1358],{},[66,1356,1357],{},"Fail loudly on configuration errors."," There is a difference between \"the API is temporarily unavailable\" (keep looping, retry) and \"the token is invalid\" or \"the config file does not parse\" (no amount of retrying will help). Exit non-zero for the second kind so the supervisor's restart limits and your alerting can see it, rather than logging the same error every thirty seconds forever.",[55,1360,1362],{"id":1361},"logging-for-commands-nobody-watches","Logging for commands nobody watches",[10,1364,1365,1366,1369,1370,1373],{},"Interactive output habits work against long-running commands. Progress bars and spinners fill log files with carriage-return redraws; colour codes appear as ",[14,1367,1368],{},"\\x1b[32m"," in the journal; and the default ",[14,1371,1372],{},"print"," buffering means a crash can lose the last minutes of output entirely. Three adjustments help:",[60,1375,1376,1386,1392],{},[63,1377,1378,1381,1382,1224],{},[66,1379,1380],{},"Detect the environment."," When stderr is not a terminal, switch progress bars off and use plain, timestamped log lines. The mechanics are in ",[30,1383,1385],{"href":1384},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Fdetecting-tty-and-adapting-output\u002F","detecting a TTY and adapting output",[63,1387,1388,1391],{},[66,1389,1390],{},"Log units of work, not ticks."," One line per unit — \"synced 318 items (2 skipped) in 41s\" — is the right granularity. A line per loop iteration when nothing happened drowns the useful ones.",[63,1393,1394,1397,1398,1401,1402,1405],{},[66,1395,1396],{},"Flush promptly."," Run with ",[14,1399,1400],{},"PYTHONUNBUFFERED=1"," under supervisors, or configure logging handlers that flush after each record (the standard ",[14,1403,1404],{},"StreamHandler"," does), so the journal shows what happened right up to the moment of failure.",[10,1407,1408,1409,1413],{},"For services whose logs are shipped to a central system, ",[30,1410,1412],{"href":1411},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fstructured-json-logging-in-python-clis\u002F","structured JSON logging"," makes each line a queryable record.",[55,1415,1417],{"id":1416},"testing-long-running-commands","Testing long-running commands",[10,1419,1420],{},"Long-running code is tested by making \"long\" short and \"forever\" finite:",[60,1422,1423,1429,1435,1441],{},[63,1424,1425,1428],{},[66,1426,1427],{},"Inject the unit of work"," and count calls, so tests control what each iteration does.",[63,1430,1431,1434],{},[66,1432,1433],{},"Stop the loop from the test"," by setting the stop event from a timer thread or from inside the unit itself after N calls.",[63,1436,1437,1440],{},[66,1438,1439],{},"Use tiny intervals"," — the loop's behaviour does not change between 0.01 seconds and five minutes.",[63,1442,1443,1446],{},[66,1444,1445],{},"Test real signals once",", in a subprocess, to prove the handler is installed and the exit code is right.",[152,1448,1450],{"className":154,"code":1449,"language":156,"meta":157,"style":157},"import threading\n\nfrom mytool.loop import Stopper, run_forever\n\n\ndef test_loop_runs_until_stopped(tmp_path):\n    stopper = Stopper()\n    calls = []\n\n    def unit() -> int:\n        calls.append(1)\n        if len(calls) == 3:\n            stopper.signum = 15\n            stopper.event.set()\n        return 1\n\n    code = run_forever(unit, interval=0.01, stopper=stopper, heartbeat=tmp_path \u002F \"hb\")\n    assert len(calls) == 3\n    assert code == 143\n    assert (tmp_path \u002F \"hb\").exists()\n\n\ndef test_failures_do_not_end_the_loop():\n    stopper = Stopper()\n    calls = []\n\n    def unit() -> int:\n        calls.append(1)\n        if len(calls) \u003C 3:\n            raise RuntimeError(\"transient\")\n        stopper.event.set()\n        return 0\n\n    run_forever(unit, interval=0.01, stopper=stopper)\n    assert len(calls) == 3\n",[14,1451,1452,1458,1462,1474,1478,1482,1492,1502,1512,1516,1530,1540,1558,1568,1573,1580,1584,1627,1641,1653,1667,1671,1675,1685,1693,1701,1705,1717,1725,1740,1754,1759,1765,1769,1789],{"__ignoreMap":157},[161,1453,1454,1456],{"class":163,"line":164},[161,1455,199],{"class":174},[161,1457,218],{"class":185},[161,1459,1460],{"class":163,"line":171},[161,1461,193],{"emptyLinePlaceholder":192},[161,1463,1464,1466,1469,1471],{"class":163,"line":189},[161,1465,175],{"class":174},[161,1467,1468],{"class":185}," mytool.loop ",[161,1470,199],{"class":174},[161,1472,1473],{"class":185}," Stopper, run_forever\n",[161,1475,1476],{"class":163,"line":196},[161,1477,193],{"emptyLinePlaceholder":192},[161,1479,1480],{"class":163,"line":205},[161,1481,193],{"emptyLinePlaceholder":192},[161,1483,1484,1486,1489],{"class":163,"line":213},[161,1485,570],{"class":174},[161,1487,1488],{"class":294}," test_loop_runs_until_stopped",[161,1490,1491],{"class":185},"(tmp_path):\n",[161,1493,1494,1497,1499],{"class":163,"line":221},[161,1495,1496],{"class":185},"    stopper ",[161,1498,266],{"class":174},[161,1500,1501],{"class":185}," Stopper()\n",[161,1503,1504,1507,1509],{"class":163,"line":229},[161,1505,1506],{"class":185},"    calls ",[161,1508,266],{"class":174},[161,1510,1511],{"class":185}," []\n",[161,1513,1514],{"class":163,"line":242},[161,1515,193],{"emptyLinePlaceholder":192},[161,1517,1518,1520,1523,1526,1528],{"class":163,"line":255},[161,1519,316],{"class":174},[161,1521,1522],{"class":294}," unit",[161,1524,1525],{"class":185},"() -> ",[161,1527,352],{"class":178},[161,1529,298],{"class":185},[161,1531,1532,1535,1538],{"class":163,"line":260},[161,1533,1534],{"class":185},"        calls.append(",[161,1536,1537],{"class":178},"1",[161,1539,275],{"class":185},[161,1541,1542,1544,1547,1550,1553,1556],{"class":163,"line":278},[161,1543,458],{"class":174},[161,1545,1546],{"class":178}," len",[161,1548,1549],{"class":185},"(calls) ",[161,1551,1552],{"class":174},"==",[161,1554,1555],{"class":178}," 3",[161,1557,298],{"class":185},[161,1559,1560,1563,1565],{"class":163,"line":283},[161,1561,1562],{"class":185},"            stopper.signum ",[161,1564,266],{"class":174},[161,1566,1567],{"class":178}," 15\n",[161,1569,1570],{"class":163,"line":288},[161,1571,1572],{"class":185},"            stopper.event.set()\n",[161,1574,1575,1577],{"class":163,"line":301},[161,1576,531],{"class":174},[161,1578,1579],{"class":178}," 1\n",[161,1581,1582],{"class":163,"line":308},[161,1583,193],{"emptyLinePlaceholder":192},[161,1585,1586,1589,1591,1594,1597,1599,1602,1604,1607,1609,1612,1614,1616,1619,1622,1625],{"class":163,"line":313},[161,1587,1588],{"class":185},"    code ",[161,1590,266],{"class":174},[161,1592,1593],{"class":185}," run_forever(unit, ",[161,1595,1596],{"class":1103},"interval",[161,1598,266],{"class":174},[161,1600,1601],{"class":178},"0.01",[161,1603,1181],{"class":185},[161,1605,1606],{"class":1103},"stopper",[161,1608,266],{"class":174},[161,1610,1611],{"class":185},"stopper, ",[161,1613,1282],{"class":1103},[161,1615,266],{"class":174},[161,1617,1618],{"class":185},"tmp_path ",[161,1620,1621],{"class":174},"\u002F",[161,1623,1624],{"class":304}," \"hb\"",[161,1626,275],{"class":185},[161,1628,1629,1632,1634,1636,1638],{"class":163,"line":330},[161,1630,1631],{"class":174},"    assert",[161,1633,1546],{"class":178},[161,1635,1549],{"class":185},[161,1637,1552],{"class":174},[161,1639,1640],{"class":178}," 3\n",[161,1642,1643,1645,1648,1650],{"class":163,"line":344},[161,1644,1631],{"class":174},[161,1646,1647],{"class":185}," code ",[161,1649,1552],{"class":174},[161,1651,1652],{"class":178}," 143\n",[161,1654,1655,1657,1660,1662,1664],{"class":163,"line":367},[161,1656,1631],{"class":174},[161,1658,1659],{"class":185}," (tmp_path ",[161,1661,1621],{"class":174},[161,1663,1624],{"class":304},[161,1665,1666],{"class":185},").exists()\n",[161,1668,1669],{"class":163,"line":372},[161,1670,193],{"emptyLinePlaceholder":192},[161,1672,1673],{"class":163,"line":386},[161,1674,193],{"emptyLinePlaceholder":192},[161,1676,1677,1679,1682],{"class":163,"line":412},[161,1678,570],{"class":174},[161,1680,1681],{"class":294}," test_failures_do_not_end_the_loop",[161,1683,1684],{"class":185},"():\n",[161,1686,1687,1689,1691],{"class":163,"line":424},[161,1688,1496],{"class":185},[161,1690,266],{"class":174},[161,1692,1501],{"class":185},[161,1694,1695,1697,1699],{"class":163,"line":429},[161,1696,1506],{"class":185},[161,1698,266],{"class":174},[161,1700,1511],{"class":185},[161,1702,1703],{"class":163,"line":455},[161,1704,193],{"emptyLinePlaceholder":192},[161,1706,1707,1709,1711,1713,1715],{"class":163,"line":470},[161,1708,316],{"class":174},[161,1710,1522],{"class":294},[161,1712,1525],{"class":185},[161,1714,352],{"class":178},[161,1716,298],{"class":185},[161,1718,1719,1721,1723],{"class":163,"line":479},[161,1720,1534],{"class":185},[161,1722,1537],{"class":178},[161,1724,275],{"class":185},[161,1726,1727,1729,1731,1733,1736,1738],{"class":163,"line":492},[161,1728,458],{"class":174},[161,1730,1546],{"class":178},[161,1732,1549],{"class":185},[161,1734,1735],{"class":174},"\u003C",[161,1737,1555],{"class":178},[161,1739,298],{"class":185},[161,1741,1742,1744,1747,1749,1752],{"class":163,"line":500},[161,1743,473],{"class":174},[161,1745,1746],{"class":178}," RuntimeError",[161,1748,759],{"class":185},[161,1750,1751],{"class":304},"\"transient\"",[161,1753,275],{"class":185},[161,1755,1756],{"class":163,"line":505},[161,1757,1758],{"class":185},"        stopper.event.set()\n",[161,1760,1761,1763],{"class":163,"line":514},[161,1762,531],{"class":174},[161,1764,554],{"class":178},[161,1766,1767],{"class":163,"line":528},[161,1768,193],{"emptyLinePlaceholder":192},[161,1770,1771,1774,1776,1778,1780,1782,1784,1786],{"class":163,"line":557},[161,1772,1773],{"class":185},"    run_forever(unit, ",[161,1775,1596],{"class":1103},[161,1777,266],{"class":174},[161,1779,1601],{"class":178},[161,1781,1181],{"class":185},[161,1783,1606],{"class":1103},[161,1785,266],{"class":174},[161,1787,1788],{"class":185},"stopper)\n",[161,1790,1791,1793,1795,1797,1799],{"class":163,"line":562},[161,1792,1631],{"class":174},[161,1794,1546],{"class":178},[161,1796,1549],{"class":185},[161,1798,1552],{"class":174},[161,1800,1640],{"class":178},[55,1802,1804],{"id":1803},"key-takeaways","Key takeaways",[60,1806,1807,1813,1816,1819,1822,1825],{},[63,1808,1809,1810,1812],{},"Treat ",[14,1811,71],{}," exactly like Ctrl+C: set a stop event, finish the current unit, clean up, exit 128 + signal.",[63,1814,1815],{},"Build loops from bounded units of work and interruptible waits.",[63,1817,1818],{},"In watch mode, debounce, filter out your own outputs and keep watching after failures.",[63,1820,1821],{},"Before scheduling, make commands non-interactive, idempotent, overlap-safe and path-explicit.",[63,1823,1824],{},"Prefer systemd timers to cron where available for logging, catch-up and overlap protection.",[63,1826,1827,1828,1830],{},"Publish progress with a heartbeat and a ",[14,1829,107],{}," command; use a dead-man switch for scheduled jobs.",[55,1832,1834],{"id":1833},"frequently-asked-questions","Frequently asked questions",[1836,1837,1839],"h3",{"id":1838},"should-a-long-running-command-daemonise-itself","Should a long-running command daemonise itself?",[10,1841,1842,1843,1846],{},"No. Double-forking into the background is a relic of pre-systemd init systems. Run in the foreground, log to stderr, and let systemd, Docker, Kubernetes or ",[14,1844,1845],{},"nohup"," handle backgrounding and restarts. Supervisors expect foreground processes and handle them better.",[1836,1848,1850],{"id":1849},"should-i-write-a-pid-file","Should I write a PID file?",[10,1852,1853,1854,1857],{},"Only if something needs it. A lock file (via ",[14,1855,1856],{},"filelock",") does the job of preventing a second instance more reliably, because the operating system releases it when the process dies; stale PID files do not clean themselves up.",[1836,1859,1861],{"id":1860},"how-do-i-reload-configuration-without-restarting","How do I reload configuration without restarting?",[10,1863,1864,1865,1868],{},"By convention, ",[14,1866,1867],{},"SIGHUP"," means \"reload\". Handle it by setting a separate event that the loop checks between units, re-reading config there. For most CLIs, a restart by the supervisor is simpler and just as quick.",[1836,1870,1872],{"id":1871},"is-asyncio-better-than-threads-for-a-long-running-command","Is asyncio better than threads for a long-running command?",[10,1874,1875,1876,1879],{},"For a loop that mostly waits on network I\u002FO and must stop promptly, asyncio's cancellation is excellent — see ",[30,1877,1878],{"href":42},"cancelling async tasks on Ctrl+C",". For a loop around synchronous libraries, the thread-and-event approach above is simpler and entirely adequate.",[1836,1881,1883],{"id":1882},"should-one-cli-command-run-the-loop-or-should-i-ship-a-separate-service","Should one CLI command run the loop, or should I ship a separate service?",[10,1885,1886,1887,1181,1890,1181,1893,1896],{},"Start with a subcommand — ",[14,1888,1889],{},"mytool serve",[14,1891,1892],{},"mytool worker",[14,1894,1895],{},"mytool sync --forever"," — which reuses the configuration, credentials and logging the rest of the tool already has, and installs with the same package. Split it into its own service only when its deployment, scaling or dependencies genuinely diverge from the CLI's. Keeping the loop in the CLI also means a developer can run exactly the same code locally that production runs under systemd.",[1836,1898,1900],{"id":1899},"how-long-should-the-grace-period-be","How long should the grace period be?",[10,1902,1903],{},"Long enough for your slowest unit of work plus cleanup, with margin, and no longer than your supervisor allows. If a unit can outlast the grace period, make the unit itself check the stop event, or make the work idempotent so being killed mid-unit is harmless.",[55,1905,1907],{"id":1906},"related","Related",[60,1909,1910,1915,1920,1924,1928,1932,1939],{},[63,1911,1912,1913],{},"Up: ",[30,1914,33],{"href":32},[63,1916,1917,1918],{},"Down: ",[30,1919,894],{"href":893},[63,1921,1917,1922],{},[30,1923,1189],{"href":1188},[63,1925,1917,1926],{},[30,1927,1263],{"href":1262},[63,1929,1917,1930],{},[30,1931,1308],{"href":1307},[63,1933,1934,1935],{},"Sideways: ",[30,1936,1938],{"href":1937},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002F","Concurrency and async in Python CLIs",[63,1940,1934,1941],{},[30,1942,1944],{"href":1943},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002F","Error handling and exit codes",[1946,1947,1948],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":157,"searchDepth":171,"depth":171,"links":1950},[1951,1952,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1970],{"id":57,"depth":171,"text":58},{"id":111,"depth":171,"text":112},{"id":143,"depth":171,"text":144},{"id":835,"depth":171,"text":836},{"id":898,"depth":171,"text":899},{"id":1196,"depth":171,"text":1197},{"id":1267,"depth":171,"text":1268},{"id":1312,"depth":171,"text":1313},{"id":1361,"depth":171,"text":1362},{"id":1416,"depth":171,"text":1417},{"id":1803,"depth":171,"text":1804},{"id":1833,"depth":171,"text":1834,"children":1963},[1964,1965,1966,1967,1968,1969],{"id":1838,"depth":189,"text":1839},{"id":1849,"depth":189,"text":1850},{"id":1860,"depth":189,"text":1861},{"id":1871,"depth":189,"text":1872},{"id":1882,"depth":189,"text":1883},{"id":1899,"depth":189,"text":1900},{"id":1906,"depth":171,"text":1907},"2026-09-18","Build Python CLI commands that run for hours: graceful shutdown on SIGTERM, watch modes with watchfiles, cron and systemd scheduling, and health checks.","advanced",false,"md",{},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis",{"title":5,"description":1972},"cli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Findex",[1981,898,1982,1983,1984],"signals","scheduling","systemd","reliability","LaiY3wn3udTuW_Ieze4O6Rgczcn6J6Ue_F_wKP2uHJU",[1987,1990,1993,1996,1999,2002,2005,2008,2011,2014,2017,2020,2023,2026,2029,2032,2035,2038,2041,2044,2047,2050,2053,2056,2059,2062,2065,2068,2071,2074,2077,2080,2083,2086,2089,2092,2095,2098,2101,2104,2107,2110,2113,2116,2119,2122,2125,2128,2131,2134,2137,2140,2143,2146,2149,2152,2155,2158,2161,2164,2167,2170,2173,2176,2179,2182,2185,2188,2191,2194,2197,2200,2203,2206,2209,2212,2215,2218,2219,2222,2225,2228,2231,2234,2237,2240,2243,2246,2249,2252,2255,2258,2260,2263,2266,2269,2272,2275,2278,2281,2284,2287,2290,2293,2296,2299,2302,2305,2308,2311,2314,2317,2320,2323,2326,2329,2332,2335,2338,2341,2344,2347,2350,2353,2356,2359,2362,2365,2368,2371,2374,2377,2380,2383,2386,2389,2392,2395,2398,2401,2404,2407,2410,2413,2416,2419,2422,2425,2428,2431,2434,2437,2440,2443,2446,2449,2452,2455,2458,2461,2464,2467,2470,2473,2476,2479,2482,2485,2488,2491,2494,2497,2500,2503,2506,2509,2512,2515,2518,2521,2524,2527,2530],{"path":1988,"title":1989},"\u002Fabout","About Python CLI Toolcraft",{"path":1991,"title":1992},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies","Advanced Argument Validation Strategies",{"path":1994,"title":1995},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies\u002Fparsing-nested-json-arguments-in-python-clis","Parsing Nested JSON Args in Python CLIs",{"path":1997,"title":1998},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies\u002Fvalidating-dependent-and-conflicting-options","Validating Dependent and Conflicting CLI Options",{"path":2000,"title":2001},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies\u002Fvalidating-file-and-directory-paths-in-clis","Validating File and Directory Paths in CLIs",{"path":2003,"title":2004},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies\u002Fwriting-custom-click-parameter-types","Writing Custom Click Parameter Types",{"path":2006,"title":2007},"\u002Fadvanced-input-parsing-user-experience\u002Fbuilding-terminal-uis-with-textual\u002Fbuilding-your-first-textual-app","Building Your First Textual App for a Python CLI",{"path":2009,"title":2010},"\u002Fadvanced-input-parsing-user-experience\u002Fbuilding-terminal-uis-with-textual\u002Fchoosing-between-a-cli-a-prompt-flow-and-a-tui","Choosing Between a CLI, a Prompt Flow and a TUI",{"path":2012,"title":2013},"\u002Fadvanced-input-parsing-user-experience\u002Fbuilding-terminal-uis-with-textual","Building Terminal UIs with Textual for Python CLIs",{"path":2015,"title":2016},"\u002Fadvanced-input-parsing-user-experience\u002Fbuilding-terminal-uis-with-textual\u002Ftesting-textual-apps-with-pilot","Testing Textual Apps with Pilot",{"path":2018,"title":2019},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation\u002Fadding-examples-and-epilogs-to-help-output","Adding Examples and Epilogs to Help Output",{"path":2021,"title":2022},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation\u002Fgenerating-man-pages-and-docs-from-a-cli","Generating Man Pages and Docs from a CLI",{"path":2024,"title":2025},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation","CLI Help Output and Documentation",{"path":2027,"title":2028},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation\u002Fversioning-and-deprecating-cli-flags","Versioning and Deprecating CLI Flags",{"path":2030,"title":2031},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation\u002Fwriting-help-text-users-actually-read","Writing Help Text Users Actually Read",{"path":2033,"title":2034},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility\u002Fadapting-output-to-terminal-width","Adapting Python CLI Output to Terminal Width",{"path":2036,"title":2037},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility\u002Fdetecting-ci-environments-and-non-interactive-shells","Detecting CI Environments and Non-Interactive Shells",{"path":2039,"title":2040},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility\u002Ffixing-unicode-and-encoding-errors-on-windows","Fixing Unicode and Encoding Errors on Windows in Python CLIs",{"path":2042,"title":2043},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility","Cross-Platform Terminal Compatibility for Python CLIs",{"path":2045,"title":2046},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility\u002Frespecting-no-color-and-force-color","Respecting NO_COLOR and FORCE_COLOR in Python CLIs",{"path":2048,"title":2049},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Fchoosing-exit-codes-for-cli-tools","Choosing Exit Codes for CLI Tools",{"path":2051,"title":2052},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Fdesigning-an-exception-hierarchy-for-a-cli","Designing an Exception Hierarchy for a Python CLI",{"path":2054,"title":2055},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Ffriendly-error-messages-and-tracebacks","Friendly Error Messages and Tracebacks",{"path":2057,"title":2058},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Fhandling-keyboard-interrupt-cleanly","Handling Keyboard Interrupt Cleanly",{"path":2060,"title":2061},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes","Error Handling and Exit Codes for CLIs",{"path":2063,"title":2064},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Freporting-machine-readable-errors-in-json-mode","Reporting Machine-Readable Errors in JSON Mode",{"path":2066,"title":2067},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Fconfig-precedence-flags-env-files-defaults","Config Precedence: Flags, Env, Files, Defaults",{"path":2069,"title":2070},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Fdiscovering-project-config-files-by-walking-up-directories","Discovering Project Config Files by Walking Up Directories",{"path":2072,"title":2073},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars","Handling Config Files and Env Vars in CLIs",{"path":2075,"title":2076},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Floading-yaml-configs-safely-in-cli-apps","Loading YAML configs safely in CLI apps",{"path":2078,"title":2079},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Freading-toml-config-with-tomllib","Reading TOML Config with tomllib in Python CLIs",{"path":2081,"title":2082},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Ftyped-settings-with-pydantic-settings","Typed Settings with pydantic-settings in Python CLIs",{"path":2084,"title":2085},"\u002Fadvanced-input-parsing-user-experience","Advanced Input Parsing for Python CLIs",{"path":2087,"title":2088},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Fadding-progress-bars-and-spinners-to-python-clis","Progress Bars and Spinners for Python CLIs",{"path":2090,"title":2091},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Fbuilding-interactive-prompts-and-menus","Building Interactive Prompts and Menus in Python CLIs",{"path":2093,"title":2094},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich","Interactive Terminal UI with Rich",{"path":2096,"title":2097},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Flive-dashboards-with-rich-live","Live Dashboards with Rich Live in Python CLIs",{"path":2099,"title":2100},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Frendering-tables-and-json-with-rich","Rendering Tables and JSON with Rich",{"path":2102,"title":2103},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Ftheming-rich-output-consistently","Theming Rich Output Consistently in Python CLIs",{"path":2105,"title":2106},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis\u002Fdynamic-completion-values-from-apis-and-files","Dynamic Completion Values from APIs and Files",{"path":2108,"title":2109},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis\u002Fenabling-tab-completion-in-click-and-typer","Enabling Tab Completion in Click and Typer",{"path":2111,"title":2112},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis","Shell Completion for Python CLIs",{"path":2114,"title":2115},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis\u002Finstalling-shell-completion-for-bash-zsh-fish","Installing Shell Completion for bash, zsh, fish",{"path":2117,"title":2118},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis\u002Ftesting-shell-completion-in-python-clis","Testing Shell Completion in Python CLIs",{"path":2120,"title":2121},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fadding-trace-ids-and-context-to-cli-logs","Adding Trace IDs and Context to Python CLI Logs",{"path":2123,"title":2124},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fadding-verbose-and-quiet-logging-flags","Adding Verbose and Quiet Logging Flags",{"path":2126,"title":2127},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps","Structured Logging for CLI Apps",{"path":2129,"title":2130},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fstructured-json-logging-in-python-clis","Structured JSON Logging in Python CLIs",{"path":2132,"title":2133},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fwriting-rotating-log-files-from-a-cli","Writing Rotating Log Files from a Python CLI",{"path":2135,"title":2136},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Fdetecting-tty-and-adapting-output","Detecting a TTY and Adapting Output",{"path":2138,"title":2139},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Femitting-json-output-for-scripting","Emitting JSON Output for Scripting",{"path":2141,"title":2142},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Fhandling-broken-pipe-and-sigpipe","Handling Broken Pipe and SIGPIPE",{"path":2144,"title":2145},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes","Working with stdin, stdout and Pipes",{"path":2147,"title":2148},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Fprocessing-large-files-and-ndjson-streams","Processing Large Files and NDJSON Streams in Python CLIs",{"path":2150,"title":2151},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Freading-piped-input-in-python-clis","Reading Piped Input in Python CLIs",{"path":2153,"title":2154},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fbuilding-an-api-client-cli-with-httpx","Building an API Client CLI with httpx",{"path":2156,"title":2157},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fdownloading-files-with-progress-in-python","Downloading Files with Progress in Python CLIs",{"path":2159,"title":2160},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis","Calling HTTP APIs from Python CLIs",{"path":2162,"title":2163},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Foauth-device-flow-login-for-clis","OAuth Device Flow Login for Python CLIs",{"path":2165,"title":2166},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fpaginating-api-results-in-a-cli","Paginating API Results in a Python CLI",{"path":2168,"title":2169},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fretries-and-backoff-for-cli-http-calls","Retries and Backoff for CLI HTTP Calls",{"path":2171,"title":2172},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Fcancelling-async-tasks-on-ctrl-c","Cancelling Async Tasks on Ctrl+C in Python CLIs",{"path":2174,"title":2175},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis","Concurrency and Async in Python CLIs",{"path":2177,"title":2178},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Fmultiprocessing-for-cpu-bound-cli-tasks","Multiprocessing for CPU-Bound CLI Tasks",{"path":2180,"title":2181},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Fparallelising-cli-work-with-thread-pools","Parallelising CLI Work with Thread Pools",{"path":2183,"title":2184},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Frate-limiting-concurrent-requests-in-clis","Rate-Limiting Concurrent Requests in Python CLIs",{"path":2186,"title":2187},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Frunning-async-code-in-typer-and-click","Running Async Code in Typer and Click",{"path":2189,"title":2190},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fcross-platform-paths-with-pathlib","Cross-Platform Paths with pathlib in CLIs",{"path":2192,"title":2193},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Ffile-locking-for-concurrent-cli-runs","File Locking for Concurrent CLI Runs in Python",{"path":2195,"title":2196},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes","Filesystem Paths and Atomic Writes for CLIs",{"path":2198,"title":2199},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fsafe-temporary-files-and-directories","Safe Temporary Files and Directories in CLIs",{"path":2201,"title":2202},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fstoring-app-data-with-platformdirs","Storing CLI App Data with platformdirs",{"path":2204,"title":2205},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fwriting-files-atomically-in-python-clis","Writing Files Atomically in Python CLIs",{"path":2207,"title":2208},"\u002Fcli-runtime-systems-integration","CLI Runtime & Systems Integration for Python",{"path":2210,"title":2211},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fbuilding-a-watch-mode-with-watchfiles","Building a Watch Mode with watchfiles in Python",{"path":2213,"title":2214},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fhandling-sigterm-and-graceful-shutdown","Handling SIGTERM and Graceful Shutdown in CLIs",{"path":2216,"title":2217},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fhealth-checks-and-heartbeats-for-long-running-clis","Health Checks and Heartbeats for Long-Running CLIs",{"path":1977,"title":5},{"path":2220,"title":2221},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Frunning-a-cli-on-a-schedule-with-cron-and-systemd","Running a Python CLI on a Schedule with cron and systemd",{"path":2223,"title":2224},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Favoiding-shell-injection-in-python-clis","Avoiding Shell Injection in Python CLIs",{"path":2226,"title":2227},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Fcalling-external-commands-safely-with-subprocess","Calling External Commands Safely with subprocess",{"path":2229,"title":2230},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Fhandling-subprocess-timeouts-and-exit-codes","Handling Subprocess Timeouts and Exit Codes",{"path":2232,"title":2233},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis","Running Subprocesses from Python CLIs",{"path":2235,"title":2236},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Fstreaming-subprocess-output-in-real-time","Streaming Subprocess Output in Real Time",{"path":2238,"title":2239},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Fwrapping-git-and-other-tools-from-a-python-cli","Wrapping git and Other Tools from a Python CLI",{"path":2241,"title":2242},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis","Secrets and Credentials in Python CLIs",{"path":2244,"title":2245},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fprompting-for-passwords-securely","Prompting for Passwords Securely in Python CLIs",{"path":2247,"title":2248},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Freading-secrets-from-env-and-files","Reading Secrets from Env Vars and Files in CLIs",{"path":2250,"title":2251},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fredacting-secrets-from-cli-output-and-logs","Redacting Secrets from CLI Output and Logs",{"path":2253,"title":2254},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fstoring-tokens-with-keyring","Storing CLI Tokens Securely with keyring",{"path":2256,"title":2257},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fsupporting-multiple-profiles-and-accounts","Supporting Multiple Profiles and Accounts in CLIs",{"path":1621,"title":2259},"Python CLI Toolcraft",{"path":2261,"title":2262},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading\u002Fcaching-expensive-work-between-cli-runs","Caching Expensive Work Between Python CLI Runs",{"path":2264,"title":2265},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading","CLI Startup Performance and Lazy Loading",{"path":2267,"title":2268},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading\u002Flazy-loading-subcommands-for-faster-startup","Lazy Loading Subcommands for Faster Startup",{"path":2270,"title":2271},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading\u002Fprofiling-python-cli-startup-time","Profiling Python CLI Startup Time",{"path":2273,"title":2274},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading\u002Freducing-cli-dependency-weight","Reducing CLI Dependency Weight",{"path":2276,"title":2277},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fargparse-subparsers-for-subcommands","argparse Subparsers for Subcommands",{"path":2279,"title":2280},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fargparse-vs-click-vs-typer-comparison","argparse vs Click vs Typer Compared",{"path":2282,"title":2283},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse","Command-Line Parsing with argparse",{"path":2285,"title":2286},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fmigrating-from-argparse-to-typer","Migrating from argparse to Typer",{"path":2288,"title":2289},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fmutually-exclusive-options-in-argparse","Mutually Exclusive Options in argparse",{"path":2291,"title":2292},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fwriting-custom-argparse-actions","Writing Custom argparse Actions in Python",{"path":2294,"title":2295},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions\u002Fadding-dry-run-and-confirmation-to-destructive-commands","Adding Dry-Run and Confirmation to Destructive Commands",{"path":2297,"title":2298},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions\u002Ffollowing-posix-and-gnu-argument-conventions","Following POSIX and GNU Argument Conventions in Python",{"path":2300,"title":2301},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions\u002Fglobal-options-vs-per-command-options","Global Options vs Per-Command Options in Python CLIs",{"path":2303,"title":2304},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions","Designing CLI Interfaces and Conventions in Python",{"path":2306,"title":2307},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions\u002Fnaming-commands-and-flags-consistently","Naming Commands and Flags Consistently in Python CLIs",{"path":2309,"title":2310},"\u002Fmodern-python-cli-frameworks-architecture","Python CLI Frameworks and Architecture",{"path":2312,"title":2313},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis\u002Fdiscovering-plugins-with-entry-points","Discovering Plugins with Entry Points in Python CLIs",{"path":2315,"title":2316},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis\u002Fhook-based-plugins-with-pluggy","Hook-Based Plugins for Python CLIs with pluggy",{"path":2318,"title":2319},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis","Plugin Architectures for Extensible CLIs",{"path":2321,"title":2322},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis\u002Fversioning-a-plugin-api","Versioning a Plugin API for a Python CLI",{"path":2324,"title":2325},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis\u002Fwriting-a-plugin-for-an-existing-cli","Writing a Plugin for an Existing CLI",{"path":2327,"title":2328},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fbest-practices-for-python-cli-entry-points","Best practices for Python CLI entry points",{"path":2330,"title":2331},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fdependency-injection-patterns-for-cli-commands","Dependency Injection Patterns for CLI Commands",{"path":2333,"title":2334},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fhow-to-structure-a-large-python-cli-project","Structuring a Large Python CLI Project",{"path":2336,"title":2337},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis","Structuring Multi-Command Python CLIs",{"path":2339,"title":2340},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fsharing-common-options-across-commands","Sharing Common Options Across Python CLI Commands",{"path":2342,"title":2343},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fsharing-state-with-click-context-objects","Sharing State with Click Context Objects",{"path":2345,"title":2346},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fend-to-end-testing-an-installed-cli","End-to-End Testing an Installed Python CLI",{"path":2348,"title":2349},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications","Testing Python CLI Applications",{"path":2351,"title":2352},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fmeasuring-cli-test-coverage","Measuring CLI Test Coverage",{"path":2354,"title":2355},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fmocking-filesystem-and-network-in-cli-tests","Mocking the Filesystem and Network in CLI Tests",{"path":2357,"title":2358},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fproperty-based-testing-cli-arguments-with-hypothesis","Property-Based Testing CLI Arguments with Hypothesis",{"path":2360,"title":2361},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fsnapshot-testing-cli-output","Snapshot Testing CLI Output",{"path":2363,"title":2364},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Ftesting-click-commands-with-clirunner","Testing Click Commands with CliRunner",{"path":2366,"title":2367},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Ftesting-interactive-prompts-and-stdin","Testing Interactive Prompts and stdin",{"path":2369,"title":2370},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Fbuilding-a-cli-with-subcommands-in-click","Building a CLI with subcommands in Click",{"path":2372,"title":2373},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Fbuilding-dynamic-commands-in-click","Building Dynamic Commands in Click",{"path":2375,"title":2376},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Fconverting-a-click-app-to-typer","Converting a Click App to Typer",{"path":2378,"title":2379},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each","Typer vs Click: When to Use Each",{"path":2381,"title":2382},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Ftyper-callback-functions-explained","Typer callback functions explained",{"path":2384,"title":2385},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Fusing-annotated-options-in-typer","Using Annotated Options in Typer",{"path":2387,"title":2388},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Fautomating-releases-from-git-tags","Automating Python CLI Releases from Git Tags",{"path":2390,"title":2391},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Fcaching-uv-dependencies-in-ci","Caching uv Dependencies in CI for Python CLIs",{"path":2393,"title":2394},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis","CI\u002FCD Pipelines for Python CLIs",{"path":2396,"title":2397},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Fpublishing-to-pypi-with-trusted-publishing","Publishing a CLI to PyPI with Trusted Publishing",{"path":2399,"title":2400},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Fsmoke-testing-the-built-wheel-in-ci","Smoke-Testing the Built Wheel of a Python CLI in CI",{"path":2402,"title":2403},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Ftesting-a-cli-across-python-versions-with-github-actions","Testing a CLI Across Python Versions in GitHub Actions",{"path":2405,"title":2406},"\u002Fproject-setup-dependency-management\u002Fcli-project-scaffolding-with-cookiecutter\u002Fbuilding-a-cookiecutter-template-for-typer-clis","Building a Cookiecutter Template for Typer CLIs",{"path":2408,"title":2409},"\u002Fproject-setup-dependency-management\u002Fcli-project-scaffolding-with-cookiecutter\u002Fcopier-vs-cookiecutter-for-cli-templates","Copier vs Cookiecutter for CLI Templates",{"path":2411,"title":2412},"\u002Fproject-setup-dependency-management\u002Fcli-project-scaffolding-with-cookiecutter","CLI Project Scaffolding with Cookiecutter",{"path":2414,"title":2415},"\u002Fproject-setup-dependency-management\u002Fcli-project-scaffolding-with-cookiecutter\u002Fpost-generation-hooks-in-cli-templates","Post-Generation Hooks in Python CLI Templates",{"path":2417,"title":2418},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fbuilding-cross-platform-release-binaries-in-ci","Building Cross-Platform Release Binaries in CI",{"path":2420,"title":2421},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fbundling-a-python-cli-with-pyinstaller","Bundling a Python CLI with PyInstaller",{"path":2423,"title":2424},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fhomebrew-and-scoop-packaging-for-python-clis","Homebrew and Scoop Packaging for Python CLIs",{"path":2426,"title":2427},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries","Distributing CLIs as Standalone Binaries",{"path":2429,"title":2430},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fnuitka-vs-pyinstaller-for-python-clis","Nuitka vs PyInstaller for Python CLI Binaries",{"path":2432,"title":2433},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fshipping-a-cli-as-a-zipapp-with-shiv","Shipping a CLI as a Zipapp with shiv",{"path":2435,"title":2436},"\u002Fproject-setup-dependency-management","Project Setup & Dependency Management",{"path":2438,"title":2439},"\u002Fproject-setup-dependency-management\u002Flinting-and-type-checking-cli-code\u002Fconfiguring-ruff-for-a-cli-project","Configuring Ruff for a Python CLI Project",{"path":2441,"title":2442},"\u002Fproject-setup-dependency-management\u002Flinting-and-type-checking-cli-code\u002Fenforcing-import-boundaries-in-a-cli-codebase","Enforcing Import Boundaries in a Python CLI Codebase",{"path":2444,"title":2445},"\u002Fproject-setup-dependency-management\u002Flinting-and-type-checking-cli-code","Linting and Type-Checking Python CLI Code",{"path":2447,"title":2448},"\u002Fproject-setup-dependency-management\u002Flinting-and-type-checking-cli-code\u002Ftype-checking-click-and-typer-code-with-mypy","Type-Checking Click and Typer Code with mypy",{"path":2450,"title":2451},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs\u002Fautomating-changelogs-with-conventional-commits","Automating Changelogs with Conventional Commits",{"path":2453,"title":2454},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs\u002Fderiving-versions-from-git-tags-with-hatch-vcs","Deriving CLI Versions from Git Tags with hatch-vcs",{"path":2456,"title":2457},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs\u002Fexposing-version-info-and-build-metadata","Exposing Version Info and Build Metadata",{"path":2459,"title":2460},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs","Managing CLI Versioning & Changelogs",{"path":2462,"title":2463},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs\u002Fsemantic-versioning-policy-for-cli-tools","A Semantic Versioning Policy for CLI Tools",{"path":2465,"title":2466},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Fbuilding-wheels-and-sdists-for-python-clis","Building Wheels and sdists for Python CLIs",{"path":2468,"title":2469},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Fbundling-data-files-with-importlib-resources","Bundling Data Files with importlib.resources in CLIs",{"path":2471,"title":2472},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution","Packaging Python CLIs for Distribution",{"path":2474,"title":2475},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Finstalling-and-distributing-clis-with-pipx","Installing and Distributing CLIs with pipx",{"path":2477,"title":2478},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Fpublishing-a-python-cli-to-pypi","Publishing a Python CLI to PyPI",{"path":2480,"title":2481},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Fwriting-pyproject-toml-metadata-for-a-cli","Writing pyproject.toml Metadata for a Python CLI",{"path":2483,"title":2484},"\u002Fproject-setup-dependency-management\u002Fpoetry-workflows-for-cli-development","Poetry Workflows for CLI Development",{"path":2486,"title":2487},"\u002Fproject-setup-dependency-management\u002Fpoetry-workflows-for-cli-development\u002Fmigrating-a-cli-from-poetry-to-uv","Migrating a Python CLI from Poetry to uv",{"path":2489,"title":2490},"\u002Fproject-setup-dependency-management\u002Fpoetry-workflows-for-cli-development\u002Fpoetry-dependency-groups-for-cli-tooling","Poetry Dependency Groups for CLI Tooling",{"path":2492,"title":2493},"\u002Fproject-setup-dependency-management\u002Fpoetry-workflows-for-cli-development\u002Fpoetry-entry-points-and-scripts-for-clis","Poetry Entry Points and Scripts for CLIs",{"path":2495,"title":2496},"\u002Fproject-setup-dependency-management\u002Fpre-commit-hooks-for-cli-projects","Pre-commit Hooks for CLI Projects",{"path":2498,"title":2499},"\u002Fproject-setup-dependency-management\u002Fpre-commit-hooks-for-cli-projects\u002Fsetting-up-pre-commit-for-python-cli-repos","Setting up pre-commit for Python CLI repos",{"path":2501,"title":2502},"\u002Fproject-setup-dependency-management\u002Fpre-commit-hooks-for-cli-projects\u002Fshipping-your-cli-as-a-pre-commit-hook","Shipping Your Python CLI as a pre-commit Hook",{"path":2504,"title":2505},"\u002Fproject-setup-dependency-management\u002Fpre-commit-hooks-for-cli-projects\u002Fwriting-local-pre-commit-hooks-in-python","Writing Local pre-commit Hooks in Python",{"path":2507,"title":2508},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management","uv for Python CLI Dependency Management",{"path":2510,"title":2511},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management\u002Frunning-one-off-cli-scripts-with-uv-run","Running One-Off CLI Scripts with uv run and PEP 723",{"path":2513,"title":2514},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management\u002Fuv-init-vs-poetry-init-for-cli-tools","uv init vs poetry init for CLI tools",{"path":2516,"title":2517},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management\u002Fuv-tool-install-vs-pipx-for-clis","uv tool install vs pipx for CLIs",{"path":2519,"title":2520},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management\u002Fuv-workspaces-for-multi-package-clis","uv Workspaces for Multi-Package Python CLIs",{"path":2522,"title":2523},"\u002Fproject-setup-dependency-management\u002Fvirtual-environments-isolation-best-practices","Python CLI Env Isolation Best Practices",{"path":2525,"title":2526},"\u002Fproject-setup-dependency-management\u002Fvirtual-environments-isolation-best-practices\u002Fmanaging-virtual-environments-for-cross-platform-clis","Managing Python CLI Virtual Environments",{"path":2528,"title":2529},"\u002Fproject-setup-dependency-management\u002Fvirtual-environments-isolation-best-practices\u002Fpinning-the-python-version-for-a-cli","Pinning the Python Version for a CLI",{"path":2531,"title":2532},"\u002Fproject-setup-dependency-management\u002Fvirtual-environments-isolation-best-practices\u002Fsupporting-multiple-python-versions-with-nox","Supporting Multiple Python Versions with nox",1789736905050]