Skip to main content

Async command-line runner

Project description

ACONS

An asynchronous command-line runner for use in async web-servers like fastapi.

Sometimes, you want to run command-line utilities and expose the output to a web-server. However, it's a bit tricky to capture the console output while it's running, and also allow the web-client to kill the job mid-process.

acons can execute command-line processes as an async function, where output can be retrieved via another async function, and a kill signal can be sent at any time.

Install

pip install acons

run

Function interface:

run(command, run_dir=None, is_parse=False, job_id=None)
  • command: str of command-line command
  • run_dir: optional starting directory of the command
  • is_parse: optional flag to parse the output as tsv file into a list of dictionaries
  • job_id: optional id that will be used to flush_lines and kill_job

To use in an async function:

await acons.run('ls', job_id='my-special-id-123')

To test the output in a sync version:

import acons
output = acons.sync_run('ls')

flush_lines

In an async function:

lines = await acons.flush_lines('my-special-id-123')

This will return the console lines produced by the job since the last call to acons.flush_lines

kill_job

In an async function:

`await kill_job('my-special-id-123')

Sends the kill signal for the job.

Parameters d

acons.RUN_INTERVAL_IN_S = 0.2 - determines how long the function polls for each interval

acons.SLEEP_INTERVAL_IN_S = 0.5 - determines how long the function sleeps between intervals

acons.N_MAX_JOB = 100 - maximum number of jobs that stores output using an LRU cache

Example server

A simple fastapi-server/vue-client is included that demonstrates acons in action.

Concurrent command-line commands can be typed in and executed. The console output will be piped back into the web-client, and the job can be cancelled at any time.

When the job is completed, the entire console output is made available through the Monaco web-editor .

Download the .zip version of this package, then run the server:

./test-server.sh

which is essentially:

uvicorn server:app --reload --port=5200

Communication between client/server is via an rpc-json interface as described in rpcseed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

acons-0.1.1.tar.gz (8.7 kB view details)

Uploaded Source

File details

Details for the file acons-0.1.1.tar.gz.

File metadata

  • Download URL: acons-0.1.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.5

File hashes

Hashes for acons-0.1.1.tar.gz
Algorithm Hash digest
SHA256 44edce3d61ec917437ac42209bef703feebc20b2d1eafcfad89a1da8873f9b28
MD5 d8d981efc8c5e5bda0db76879899e4e8
BLAKE2b-256 510b5de7a8ad50c4e74efcf22eb444dea59c45797ca8f882164b4d16b7f1b680

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page