detachedproc


Namedetachedproc JSON
Version 0.12 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/detachedproc
SummarySimilar to subprocess, but with 100% detached processes
upload_time2023-09-17 01:46:28
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords detached process subprocess
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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.


```python
# 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.
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/detachedproc",
    "name": "detachedproc",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "detached,process,subprocess",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/62/6d/1c204a9c6faa229455b1a4872047ec9ce569a70051ce12a036b4d36768e3/detachedproc-0.12.tar.gz",
    "platform": null,
    "description": "\r\n# Similar to subprocess, but with 100% detached processes\r\n\r\n## Tested against Windows 10 / Python 3.11 / Anaconda and Ubuntu\r\n\r\n### pip install detachedproc\r\n\r\n\r\nThe module defines a Python class named DetachedPopen that enable the execution and management \r\nof detached processes, primarily on Windows but with some compatibility for Unix-like systems as well.\r\n\r\nHere's an overview of what the module does:\r\n\r\n## Detached Process Execution: \r\n\r\nThe main purpose of the module is to execute external processes in a detached manner, \r\nmeaning the processes run independently of the Python script, \r\nand the script can continue its execution without waiting for the process to finish.\r\n\r\n## Windows Compatibility: \r\n\r\nThe module provides options for running processes with various Windows-specific features, \r\nsuch as specifying window styles (\"Normal,\" \"Hidden,\" \"Minimized,\" \"Maximized\") \r\nand verbs (used in shell execution).\r\n\r\n## Input and Output Handling: \r\n\r\nIt allows you to specify the standard input of the process and capture its standard output and standard error. \r\nYou can print or capture the output as needed. \r\nAdditionally, it supports buffering for both standard output and standard error.\r\n\r\n## Process Monitoring: \r\n\r\nThe module uses the psutil library to monitor and manage processes. \r\nIt can track child processes spawned by the executed command.\r\n\r\n## Temporary File Management: \r\n\r\nTemporary files are created for various purposes (e.g., passing input, capturing output) \r\nduring process execution. You have the option to delete these temporary files when they are no longer needed.\r\n\r\n## Compatibility with subprocess: \r\n\r\nThe module is designed with some compatibility with the subprocess.Popen class, \r\nallowing users familiar with the standard library subprocess module to transition to \r\nthis more specialized functionality while maintaining a similar interface.\r\n\r\n# Advantages:\r\n\r\n## Detached Execution: \r\n\r\nThis 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.\r\n\r\n## Windows Integration: \r\n\r\nIt offers features specifically tailored for Windows users, such as controlling window styles and verbs when executing processes.\r\n\r\n## Input and Output Handling: \r\n\r\nIt provides flexibility in handling process input and capturing output, which can be essential when dealing with command-line tools and automation.\r\n\r\n\r\n```python\r\n# Important! \"powershell.exe -ExecutionPolicy RemoteSigned Start-Process ...\" is used to execute the subprocess, you might need to allow that.\r\n\r\n\r\nimport subprocess\r\nfrom time import sleep\r\nfrom detachedproc import DetachedPopen\r\n\r\np=DetachedPopen(\r\n    args=[rf\"ping.exe\",'-n','10000', \"8.8.8.8\"],\r\n    bufsize=-1,\r\n    executable=None,\r\n    stdin=None,\r\n    stdout=subprocess.PIPE,\r\n    stderr=subprocess.PIPE,\r\n    preexec_fn=None,\r\n    close_fds=True,\r\n    shell=False,\r\n    cwd=None,\r\n    env=None,\r\n    universal_newlines=None,\r\n    startupinfo=None,\r\n    creationflags=0,\r\n    restore_signals=True,\r\n    start_new_session=False,\r\n    pass_fds=(),\r\n    user=None,\r\n    group=None,\r\n    extra_groups=None,\r\n    encoding=None,\r\n    errors=None,\r\n    text=None,\r\n    umask=-1,\r\n    pipesize=-1,\r\n    window_style=\"Hidden\",\r\n    wait=False,\r\n    verb=None,\r\n    what_if=False,\r\n    print_stdout=False,\r\n    print_stderr=False,\r\n    capture_stdout=True,\r\n    capture_stderr=True,\r\n    stdoutbuffer=None,\r\n    stderrbuffer=None,\r\n    psutil_timeout=15,\r\n    delete_tempfiles=True,\r\n\tread_stdout_stderr_async=False,\r\n\targs_to_83=True,\r\n)\r\nwhile True:\r\n    print(p.stdout.read())\r\n    sleep(1)\r\n\t\r\n\t\r\n\t\r\n\t\r\nA class for launching and managing detached processes.\r\n\r\nThis class provides a way to execute processes in the background, similar to the `subprocess.Popen` class.\r\nIt allows you to run processes with various options and provides methods for controlling and monitoring them.\r\n\r\nArgs:\r\n\targs (list or str): The command to be executed as a list of arguments or a single string to be parsed.\r\n\tbufsize (int): Not used; included for compatibility with `subprocess.Popen`.\r\n\texecutable (str): Not used; included for compatibility with `subprocess.Popen`.\r\n\tstdin (str or None): Input to be provided to the process's standard input. Default is None.\r\n\tstdout (None or subprocess.PIPE): The standard output of the process. Default is None.\r\n\tstderr (None or subprocess.PIPE): The standard error of the process. Default is None.\r\n\tpreexec_fn (callable or None): Not used; included for compatibility with `subprocess.Popen`.\r\n\tclose_fds (bool): Not used; included for compatibility with `subprocess.Popen`.\r\n\tshell (bool): Not used; included for compatibility with `subprocess.Popen`.\r\n\tcwd (str or None): The working directory for the process. Default is None.\r\n\tenv (dict or None): The environment variables to be used for the process. Default is None.\r\n\tuniversal_newlines (bool or None): Not used; included for compatibility with `subprocess.Popen`.\r\n\tstartupinfo (subprocess.STARTUPINFO): Not used; included for compatibility with `subprocess.Popen`.\r\n\tcreationflags (int): Not used; included for compatibility with `subprocess.Popen`.\r\n\trestore_signals (bool): Not used; included for compatibility with `subprocess.Popen`.\r\n\tstart_new_session (bool): Not used; included for compatibility with `subprocess.Popen`.\r\n\tpass_fds (tuple): Not used; included for compatibility with `subprocess.Popen`.\r\n\tuser (str or None): Not used; included for compatibility with `subprocess.Popen`.\r\n\tgroup (str or None): Not used; included for compatibility with `subprocess.Popen`.\r\n\textra_groups (list or None): Not used; included for compatibility with `subprocess.Popen`.\r\n\tencoding (str or None): Not used; included for compatibility with `subprocess.Popen`.\r\n\terrors (str or None): Not used; included for compatibility with `subprocess.Popen`.\r\n\ttext (bool or None): Not used; included for compatibility with `subprocess.Popen`.\r\n\tumask (int): Not used; included for compatibility with `subprocess.Popen`.\r\n\tpipesize (int): Not used; included for compatibility with `subprocess.Popen`.\r\n\twait (bool): Whether to wait for the process to complete before returning. Default is False.\r\n\tverb (str or None): The verb to use when executing the process (Windows only). Default is None.\r\n\twhat_if (bool): Whether to simulate the execution of the process without actually running it (Windows only).\r\n\t\tDefault is False.\r\n\twindow_style (str): The window style for the process (Windows only).\r\n\t\tPossible values: \"Normal\", \"Hidden\", \"Minimized\", \"Maximized\". Default is \"Normal\".\r\n\tprint_stdout (bool): Whether to print the process's standard output. Default is True.\r\n\tprint_stderr (bool): Whether to print the process's standard error. Default is True.\r\n\tcapture_stdout (bool): Whether to capture the process's standard output. Default is True.\r\n\tcapture_stderr (bool): Whether to capture the process's standard error. Default is True.\r\n\tstdoutbuffer (int or None): The maximum number of lines to buffer for standard output. Default is None.\r\n\tstderrbuffer (int or None): The maximum number of lines to buffer for standard error. Default is None.\r\n\tpsutil_timeout (int): The maximum time to wait for process information using psutil. Default is 15 seconds.\r\n\tdelete_tempfiles (bool): Whether to delete temporary files created during execution. Default is True.\r\n\tread_stdout_stderr_async (bool): Whether to read stdout/stderr during execution (True means slower execution).\r\n\t\t\t\t\t\t\t\t\t Default is False\r\n\targs_to_83 (bool): Whether to convert arguments to 8.3 format (Windows only) - The main executable will\r\n\t\t\t\t\t   always be converted to 8.3 format, this is only for the arguments afterwards (args[1:]).\r\n\t\t\t\t\t   Default is True.\r\n\r\nMethods:\r\n\tterminate(): Terminate the process.\r\n\tkill(): Terminate the process.\r\n\tsend_signal(): Terminate the process.\r\n\r\n\tTo read standard output:\r\n\r\n\tstdout.read(): Read the process's standard output.\r\n\tstdout.readlines(): Read the process's standard output as a list of lines.\r\n\tstdout.readline(): Read a single line from the process's standard output.\r\n\r\n\tTo read standard error:\r\n\r\n\tstderr.read(): Read the process's standard error.\r\n\tstderr.readlines(): Read the process's standard error as a list of lines.\r\n\tstderr.readline(): Read a single line from the process's standard error.\r\n\r\n\r\nTo use it on Ubuntu\r\n\r\n# Update the list of packages\r\nsudo apt-get update\r\n# Install pre-requisite packages.\r\nsudo apt-get install -y wget apt-transport-https software-properties-common\r\n# Download the Microsoft repository GPG keys\r\nwget -q \"https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb\"\r\n# Register the Microsoft repository GPG keys\r\nsudo dpkg -i packages-microsoft-prod.deb\r\n# Delete the the Microsoft repository GPG keys file\r\nrm packages-microsoft-prod.deb\r\n# Update the list of packages after we added packages.microsoft.com\r\nsudo apt-get update\r\n# Install PowerShell\r\nsudo apt-get install -y powershell\r\n# Start PowerShell\r\npwsh\r\n# As superuser, register the Microsoft repository once. After registration, you can update PowerShell with \r\nsudo apt-get install powershell.\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Similar to subprocess, but with 100% detached processes",
    "version": "0.12",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/detachedproc"
    },
    "split_keywords": [
        "detached",
        "process",
        "subprocess"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1b42ed09b7b9ac9a8c03abef71d0a43c23133a22d7fc2000b49ca81f7f6c2a73",
                "md5": "c1c08297cd9e4de30446f86675d615c2",
                "sha256": "005aab42953adb2b0d60c953c670173ec26378652d6586eb857f73d40375c131"
            },
            "downloads": -1,
            "filename": "detachedproc-0.12-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c1c08297cd9e4de30446f86675d615c2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 16792,
            "upload_time": "2023-09-17T01:46:26",
            "upload_time_iso_8601": "2023-09-17T01:46:26.511358Z",
            "url": "https://files.pythonhosted.org/packages/1b/42/ed09b7b9ac9a8c03abef71d0a43c23133a22d7fc2000b49ca81f7f6c2a73/detachedproc-0.12-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "626d1c204a9c6faa229455b1a4872047ec9ce569a70051ce12a036b4d36768e3",
                "md5": "5fd9474e4b1bc230bf057f425a027220",
                "sha256": "c0174de689162a59e12329ff859852941adc11b7f0d88ff0247ef7d15bc953ab"
            },
            "downloads": -1,
            "filename": "detachedproc-0.12.tar.gz",
            "has_sig": false,
            "md5_digest": "5fd9474e4b1bc230bf057f425a027220",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 15862,
            "upload_time": "2023-09-17T01:46:28",
            "upload_time_iso_8601": "2023-09-17T01:46:28.513313Z",
            "url": "https://files.pythonhosted.org/packages/62/6d/1c204a9c6faa229455b1a4872047ec9ce569a70051ce12a036b4d36768e3/detachedproc-0.12.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-17 01:46:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "detachedproc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "detachedproc"
}
        
Elapsed time: 0.13611s