Skip to main content

Allows to execute subprocess commands and get their output in real-time.

Project description

B.ContinuousSubprocess

A helper package that lets you execute long running processes and continuously receive their output.

Description

Imagine you want to programmatically run a command that lists objects in a given directory. The command subprocess.call('ls') will do the trick quite conveniently. However, the same approach to long running commands like cdk deploy * or pytest -s or ls / -R is not very convenient as you get the whole output only when the process finishes. This package solves this inconvenience by outputting data in real-time as the process runs.

Remarks

Biomapas aims to modernise life-science industry by sharing its IT knowledge with other companies and the community. This is an open source library intended to be used by anyone. Improvements and pull requests are welcome.

Related technology

  • Python 3
  • Python subprocess Popen
  • OS processes

Assumptions

The project assumes the following:

  • You have basic knowledge in python programming.
  • You have basic knowledge in OS processes.

Useful sources

Install

The project is built and uploaded to PyPi. Install it by using pip.

pip install b-continuous-subprocess

Or directly install it through source.

pip install .

Usage & Examples

The library is extremely easy to use. Simply create an instance of ContinuousSubprocess and execute it to get a generator. Then iterate through the generator to receive your process output in real time.

from b_continuous_subprocess.continuous_subprocess import ContinuousSubprocess

command = 'cdk deploy *'
generator = ContinuousSubprocess(command).execute()

for data in generator:
    print(data, end='')

Example how to handle errors:

import json
import subprocess

from b_continuous_subprocess.continuous_subprocess import ContinuousSubprocess

continuous_process = ContinuousSubprocess('cdk deploy *')
generator = continuous_process.execute()

try:
    for line in generator:
        print(line, end='')
except subprocess.CalledProcessError as ex:
    error_output = json.loads(ex.output)
    
    # Error message.
    message = error_output['message']
    # Stack trace.
    trace = error_output['trace']
    # The length of a stack trace (in lines).
    trace_size = error_output['trace_size']
    # The maximum possible (allowed) length of a stack trace.
    max_trace_size = error_output['max_trace_size']
    
    print(message)
    
    for line in trace:
        print(line, end='')

Testing

The project has tests that can be run. These are mostly simple tests that can be run out-of-the-box.

Run tests from a root directory with pytest python testing library:

pytest

The tests usually take less than a few seconds to complete.

Contribution

Found a bug? Want to add or suggest a new feature?
Contributions of any kind are gladly welcome. You may contact us directly, create a pull-request or an issue in github platform. Lets modernize the world together.

Release history

0.4.0

  • Remove unnecessary testing dependencies.
  • Improve CI/CD pipelines.

0.3.2

  • More logging.
  • More debugging.
  • Fix an actual bug where the std streams que might be empty.

0.3.1

  • Cleanup process.

0.3.0

  • Add ability to control stack trace size.

0.2.1

  • Deleted all .idea files from the project.

0.2.0

  • Add stack trace when the error has happened.

0.1.2

  • Rethink the way stdout and stderr are yielded.

0.1.1

  • Pipe errors too.

0.1.0

  • Add ability to kill a process.

0.0.3

  • Start using Pylint.
  • Start using Black.

0.0.2

  • Finalize pipelines.
  • Finalize project structure and naming.

0.0.1

  • Initial build.

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

b_continuous_subprocess-0.4.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

b_continuous_subprocess-0.4.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file b_continuous_subprocess-0.4.0.tar.gz.

File metadata

File hashes

Hashes for b_continuous_subprocess-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e813a73b7e370ba7e34da7874a557e8ac8d16ceb2c2167a46245df30543e106a
MD5 db52d32e032fa925c2f09502984fe4ad
BLAKE2b-256 f533b894396b0445c22ab4dfe9486b9bdf6bc5012c9202bd9b40ac48870d060c

See more details on using hashes here.

Provenance

File details

Details for the file b_continuous_subprocess-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for b_continuous_subprocess-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9e38d4e2ad6261c2ae6e865e5f8cc1013c104b3812c2492853a2da5d166a007
MD5 3585e3f32fd11bc7e9c3b17844a61877
BLAKE2b-256 214a4c960de79674e0db81c753f8e0ce91ccbd1d80c9edd8ad8d07eb4a1a4009

See more details on using hashes here.

Provenance

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