Assorted process and subprocess management functions.
Project description
Assorted process and subprocess management functions.
Latest release 20220606:
- run: fold in the superior run() from cs.ebooks.
- pipefrom,pipeto: replace trace= with quiet= like run().
- print_argv: add an
endparameter (used by pipefrom).
Function groupargv(pre_argv, argv, post_argv=(), max_argv=None, encode=False)
Distribute the array argv over multiple arrays
to fit within MAX_ARGV.
Return a list of argv lists.
Parameters:
pre_argv: the sequence of leading argumentsargv: the sequence of arguments to distribute; this may not be emptypost_argv: optional, the sequence of trailing argumentsmax_argv: optional, the maximum length of each distributed argument list, default: MAX_ARGVencode: default False. If true, encode the argv sequences into bytes for accurate tallying. Ifencodeis a Boolean, encode the elements with their .encode() method. Ifencodeis astr, encode the elements with their.encode()method withencodeas the encoding name; otherwise presume thatencodeis a callable for encoding each element.
The returned argv arrays will contain the encoded element values.
Function PidFileManager(path, pid=None)
Context manager for a pid file.
Parameters:
path: the path to the process id file.pid: the process id to store in the pid file, default fromos.etpid.
Writes the process id file at the start and removes the process id file at the end.
Function pipefrom(argv, quiet=False, binary=False, keep_stdin=False, **kw)
Pipe text from a command.
Optionally trace invocation.
Return the Popen object with .stdout decoded as text.
Parameters:
-
argv: the command argument list -
binary: if true (default false) return the raw stdout instead of a text wrapper -
quiet: optional flag, defaultFalse;if
traceisTrue, recite invocation to stderr otherwise presume thattraceis a stream to which to recite the invocation. -
keep_stdin: if true (defaultFalse) do not attach the command's standard input to the null device. The default behaviour is to do so, preventing commands from accidentally consuming the main process' input stream.
Other keyword arguments are passed to the io.TextIOWrapper
which wraps the command's output.
Function pipeto(argv, quiet=False, **kw)
Pipe text to a command. Optionally trace invocation. Return the Popen object with .stdin encoded as text.
Parameters:
argv: the command argument listtrace: if true (defaultFalse), iftraceisTrue, recite invocation to stderr otherwise presume thattraceis a stream to which to recite the invocation.
Other keyword arguments are passed to the io.TextIOWrapper
which wraps the command's input.
Function print_argv(*argv, indent='', subindent=' ', end='\n', file=None, fold=False)
Print an indented possibly folded command line.
Function remove_pidfile(path)
Truncate and remove a pidfile, permissions permitting.
Function run(argv, doit=True, logger=None, quiet=False, **subp_options)
Run a command via subprocess.run.
Return the CompletedProcess result or None if doit is false.
Parameters:
argv: the command line to rundoit: optional flag, defaultTrue; if false do not run the command and returnNonelogger: optional logger, defaultNone; ifTrue, uselogging.getLogger(); if notNoneorFalsetrace usingprint_argvquiet: defaultFalse; if true, do not print the command or its outputsubp_options: optional mapping of keyword arguments to pass tosubprocess.run
Function signal_handler(sig, handler, call_previous=False)
Context manager to push a new signal handler,
yielding the old handler,
restoring the old handler on exit.
If call_previous is true (default False)
also call the old handler after the new handler on receipt of the signal.
Parameters:
sig: theintsignal number to catchhandler: the handler function to call with(sig,frame)call_previous: optional flag (defaultFalse); if true, also call the old handler (if any) afterhandler
Function signal_handlers(sig_hnds, call_previous=False, _stacked=None)
Context manager to stack multiple signal handlers,
yielding a mapping of sig=>old_handler.
Parameters:
sig_hnds: a mapping ofsig=>new_handleror an iterable of(sig,new_handler)pairscall_previous: optional flag (defaultFalse), passed tosignal_handler()
Function stop(pid, signum=<Signals.SIGTERM: 15>, wait=None, do_SIGKILL=False)
Stop the process specified by pid, optionally await its demise.
Parameters:
pid: process id. Ifpidis a string, treat as a process id file and read the process id from it.signum: the signal to send, defaultsignal.SIGTERM.wait: whether to wait for the process, defaultNone. IfNone, returnTrue(signal delivered). If0, wait indefinitely until the process exits as tested byos.kill(pid, 0). If greater than 0, wait up towaitseconds for the process to die; if it exits, returnTrue, otherwiseFalse;do_SIGKILL: if true (defaultFalse), send the processsignal.SIGKILLas a final measure before return.
Function write_pidfile(path, pid=None)
Write a process id to a pid file.
Parameters:
path: the path to the pid file.pid: the process id to write, defautl fromos.getpid.
Release Log
Release 20220606:
- run: fold in the superior run() from cs.ebooks.
- pipefrom,pipeto: replace trace= with quiet= like run().
- print_argv: add an
endparameter (used by pipefrom).
Release 20220531:
- New print_argv function for writing shell command lines out nicely.
- Bump requirements for cs.gimmicks.
Release 20220504: signal_handlers: reshape try/except to avoid confusing traceback.
Release 20220429:
- New signal_handler(sig,handler,call_previous=False) context manager to push a signal handler.
- New signal_handlers() context manager to stack handlers for multiple signals.
Release 20190101: Bugfix context manager cleanup. groupargv improvements.
Release 20171112: Bugfix array length counting.
Release 20171110: New function groupargv for breaking up argv lists to fit within the maximum argument limit; constant MAX_ARGV for the default limit.
Release 20171031: run: accept optional pids parameter, a setlike collection of process ids.
Release 20171018:
run: replace trace parameter with logger, default None
Release 20170908.1: remove dependency on cs.pfx
Release 20170908: run: pass extra keyword arguments to subprocess.call
Release 20170906.1: Add run, pipefrom and pipeto - were incorrectly in another module.
Release 20170906: First PyPI release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cs.psutils-20220606.tar.gz.
File metadata
- Download URL: cs.psutils-20220606.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.1 readme-renderer/30.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.3.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0fd19a05a840cd7c886f8ba41bdabe6b926ba8159ece35eb2c252ba1a725e0b0
|
|
| MD5 |
34077de7f03a966c8fdff50ef7565f9a
|
|
| BLAKE2b-256 |
df07a716e2bbfad14a385707cc713ed782d3e8e9c46472221af4d5dd2c61475e
|
File details
Details for the file cs.psutils-20220606-py3-none-any.whl.
File metadata
- Download URL: cs.psutils-20220606-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.1 readme-renderer/30.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.3.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04d657658bdeae604d97ed644c80b3afc1edc2228bcb7a796cf5aabd9fa22b92
|
|
| MD5 |
d5c109b411f43f05e84e148ffc37c3da
|
|
| BLAKE2b-256 |
2674a90e12fb156c3e285d7083fa2d2a998a85d02e904eaa1478f6ca17972fcc
|