https://github.com/isaaskin/command_spawner
Command Spawner is a non-blocking command runner library for Python
https://github.com/isaaskin/command_spawner
callback command command-spawner non-blocking python spawn spawner subprocess
Last synced: 10 months ago
JSON representation
Command Spawner is a non-blocking command runner library for Python
- Host: GitHub
- URL: https://github.com/isaaskin/command_spawner
- Owner: isaaskin
- License: mit
- Created: 2020-12-12T20:15:54.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-14T14:12:07.000Z (almost 5 years ago)
- Last Synced: 2025-02-26T20:06:22.653Z (10 months ago)
- Topics: callback, command, command-spawner, non-blocking, python, spawn, spawner, subprocess
- Language: Python
- Homepage:
- Size: 20.5 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/isaaskin/command_spawner/actions/workflows/python-package.yml)
[](https://opensource.org/licenses/MIT)
# Command Spawner
Command Spawner is a non-blocking command runner library for Python.
Command Spawner runs commands at background and provides live output, error and finish data through the provided callback functions.
# Installing
Install and update using pip:
```bash
$ pip install command-spawner
```
# Example usage
```python
# Triggered when received new stdout
def on_output(data):
print(f"Received data: {data}")
# Triggered when received new stderr
def on_error(data):
print(f"Received error: {data}")
# Triggered when the command execution has finished
def on_finished(data):
print(f"Finished with return code: {data}")
# Triggered when an exception has been thrown from the process module
def on_exception(exception):
print(f"Received exception: {exception}")
# or
raise exception
command_spawner = CommandSpawner(command="ping google.com",
on_output_callback=on_output, # Suppress output when it is not provided
on_error_callback=on_error, # Suppress error when it is not provided
on_finished_callback=on_finished, # Suppress return code when it is not provided
on_exception_callback=on_exception, # Raise exception on runtime when it is not provided
shell=False, # If True, the command will be executed through the shell (not recommended)
daemon=False # If True, current runtime will not wait for commands to be executed
)
# The method to run the process
command_spawner.run()
# The method to kill the current process
command_spawner.kill()
# The method to wait for the process to be finished
command_spawner.wait()
```
Enjoy!