Skip to main content

Similar to subprocess, but with 100% detached processes

Project description

Similar to subprocess, but with 100% detached processes

Tested against Windows 10 / Python 3.11 / Anaconda and Ubuntu

pip install detachedproc

The module defines a Python class named DetachedPopen that enable the execution and management of detached processes, primarily on Windows but with some compatibility for Unix-like systems as well.

Here's an overview of what the module does:

Detached Process Execution:

The main purpose of the module is to execute external processes in a detached manner, meaning the processes run independently of the Python script, and the script can continue its execution without waiting for the process to finish.

Windows Compatibility:

The module provides options for running processes with various Windows-specific features, such as specifying window styles ("Normal," "Hidden," "Minimized," "Maximized") and verbs (used in shell execution).

Input and Output Handling:

It allows you to specify the standard input of the process and capture its standard output and standard error. You can print or capture the output as needed. Additionally, it supports buffering for both standard output and standard error.

Process Monitoring:

The module uses the psutil library to monitor and manage processes. It can track child processes spawned by the executed command.

Temporary File Management:

Temporary files are created for various purposes (e.g., passing input, capturing output) during process execution. You have the option to delete these temporary files when they are no longer needed.

Compatibility with subprocess:

The module is designed with some compatibility with the subprocess.Popen class, allowing users familiar with the standard library subprocess module to transition to this more specialized functionality while maintaining a similar interface.

Advantages:

Detached Execution:

This module is particularly useful when you need to run external commands or processes that might take a long time to complete, and you want your Python script to continue executing other tasks without waiting for the external process.

Windows Integration:

It offers features specifically tailored for Windows users, such as controlling window styles and verbs when executing processes.

Input and Output Handling:

It provides flexibility in handling process input and capturing output, which can be essential when dealing with command-line tools and automation.

# Important! "powershell.exe -ExecutionPolicy RemoteSigned Start-Process ..." is used to execute the subprocess, you might need to allow that.


import subprocess
from time import sleep
from detachedproc import DetachedPopen

p=DetachedPopen(
    args=[rf"ping.exe",'-n','10000', "8.8.8.8"],
    bufsize=-1,
    executable=None,
    stdin=None,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    preexec_fn=None,
    close_fds=True,
    shell=False,
    cwd=None,
    env=None,
    universal_newlines=None,
    startupinfo=None,
    creationflags=0,
    restore_signals=True,
    start_new_session=False,
    pass_fds=(),
    user=None,
    group=None,
    extra_groups=None,
    encoding=None,
    errors=None,
    text=None,
    umask=-1,
    pipesize=-1,
    window_style="Hidden",
    wait=False,
    verb=None,
    what_if=False,
    print_stdout=False,
    print_stderr=False,
    capture_stdout=True,
    capture_stderr=True,
    stdoutbuffer=None,
    stderrbuffer=None,
    psutil_timeout=15,
    delete_tempfiles=True,
	read_stdout_stderr_async=False,
	args_to_83=True,
)
while True:
    print(p.stdout.read())
    sleep(1)
	
	
	
	
A class for launching and managing detached processes.

This class provides a way to execute processes in the background, similar to the `subprocess.Popen` class.
It allows you to run processes with various options and provides methods for controlling and monitoring them.

Args:
	args (list or str): The command to be executed as a list of arguments or a single string to be parsed.
	bufsize (int): Not used; included for compatibility with `subprocess.Popen`.
	executable (str): Not used; included for compatibility with `subprocess.Popen`.
	stdin (str or None): Input to be provided to the process's standard input. Default is None.
	stdout (None or subprocess.PIPE): The standard output of the process. Default is None.
	stderr (None or subprocess.PIPE): The standard error of the process. Default is None.
	preexec_fn (callable or None): Not used; included for compatibility with `subprocess.Popen`.
	close_fds (bool): Not used; included for compatibility with `subprocess.Popen`.
	shell (bool): Not used; included for compatibility with `subprocess.Popen`.
	cwd (str or None): The working directory for the process. Default is None.
	env (dict or None): The environment variables to be used for the process. Default is None.
	universal_newlines (bool or None): Not used; included for compatibility with `subprocess.Popen`.
	startupinfo (subprocess.STARTUPINFO): Not used; included for compatibility with `subprocess.Popen`.
	creationflags (int): Not used; included for compatibility with `subprocess.Popen`.
	restore_signals (bool): Not used; included for compatibility with `subprocess.Popen`.
	start_new_session (bool): Not used; included for compatibility with `subprocess.Popen`.
	pass_fds (tuple): Not used; included for compatibility with `subprocess.Popen`.
	user (str or None): Not used; included for compatibility with `subprocess.Popen`.
	group (str or None): Not used; included for compatibility with `subprocess.Popen`.
	extra_groups (list or None): Not used; included for compatibility with `subprocess.Popen`.
	encoding (str or None): Not used; included for compatibility with `subprocess.Popen`.
	errors (str or None): Not used; included for compatibility with `subprocess.Popen`.
	text (bool or None): Not used; included for compatibility with `subprocess.Popen`.
	umask (int): Not used; included for compatibility with `subprocess.Popen`.
	pipesize (int): Not used; included for compatibility with `subprocess.Popen`.
	wait (bool): Whether to wait for the process to complete before returning. Default is False.
	verb (str or None): The verb to use when executing the process (Windows only). Default is None.
	what_if (bool): Whether to simulate the execution of the process without actually running it (Windows only).
		Default is False.
	window_style (str): The window style for the process (Windows only).
		Possible values: "Normal", "Hidden", "Minimized", "Maximized". Default is "Normal".
	print_stdout (bool): Whether to print the process's standard output. Default is True.
	print_stderr (bool): Whether to print the process's standard error. Default is True.
	capture_stdout (bool): Whether to capture the process's standard output. Default is True.
	capture_stderr (bool): Whether to capture the process's standard error. Default is True.
	stdoutbuffer (int or None): The maximum number of lines to buffer for standard output. Default is None.
	stderrbuffer (int or None): The maximum number of lines to buffer for standard error. Default is None.
	psutil_timeout (int): The maximum time to wait for process information using psutil. Default is 15 seconds.
	delete_tempfiles (bool): Whether to delete temporary files created during execution. Default is True.
	read_stdout_stderr_async (bool): Whether to read stdout/stderr during execution (True means slower execution).
									 Default is False
	args_to_83 (bool): Whether to convert arguments to 8.3 format (Windows only) - The main executable will
					   always be converted to 8.3 format, this is only for the arguments afterwards (args[1:]).
					   Default is True.

Methods:
	terminate(): Terminate the process.
	kill(): Terminate the process.
	send_signal(): Terminate the process.

	To read standard output:

	stdout.read(): Read the process's standard output.
	stdout.readlines(): Read the process's standard output as a list of lines.
	stdout.readline(): Read a single line from the process's standard output.

	To read standard error:

	stderr.read(): Read the process's standard error.
	stderr.readlines(): Read the process's standard error as a list of lines.
	stderr.readline(): Read a single line from the process's standard error.


To use it on Ubuntu

# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Delete the the Microsoft repository GPG keys file
rm packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh
# As superuser, register the Microsoft repository once. After registration, you can update PowerShell with 
sudo apt-get install powershell.

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

detachedproc-0.12.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

detachedproc-0.12-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file detachedproc-0.12.tar.gz.

File metadata

  • Download URL: detachedproc-0.12.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for detachedproc-0.12.tar.gz
Algorithm Hash digest
SHA256 c0174de689162a59e12329ff859852941adc11b7f0d88ff0247ef7d15bc953ab
MD5 5fd9474e4b1bc230bf057f425a027220
BLAKE2b-256 626d1c204a9c6faa229455b1a4872047ec9ce569a70051ce12a036b4d36768e3

See more details on using hashes here.

File details

Details for the file detachedproc-0.12-py3-none-any.whl.

File metadata

  • Download URL: detachedproc-0.12-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for detachedproc-0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 005aab42953adb2b0d60c953c670173ec26378652d6586eb857f73d40375c131
MD5 c1c08297cd9e4de30446f86675d615c2
BLAKE2b-256 1b42ed09b7b9ac9a8c03abef71d0a43c23133a22d7fc2000b49ca81f7f6c2a73

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