Mozprocess

1.4.0 · maintenance · verified Thu Apr 16

Mozprocess is a process-handling module developed by Mozilla, providing enhanced features beyond Python's standard `subprocess` module, such as improved child process handling (especially on Windows), flexible timeouts, and custom output handlers. While currently at version 1.4.0, the project's documentation notes it is poorly maintained and advises using Python's `subprocess` for routine needs. The project does not have a strict release cadence and updates are infrequent.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates launching a process using `mozprocess.ProcessHandler`, setting up a callback for each line of output, and defining a handler for process timeouts. It runs a simple Python script, capturing its output line by line and reporting its final status.

import sys
import os
from mozprocess import ProcessHandler

def print_output_line(line):
    print(f"Process Output: {line.strip()}")

def on_timeout_handler():
    print("Process timed out!")

# Example: Run a simple command and capture output
command = ['python', '-c', 'import time; print("Hello"); time.sleep(1); print("World");']

try:
    # Using ProcessHandler to manage the process, capture output, and handle timeouts
    p = ProcessHandler(
        command,
        processOutputLine=[print_output_line],
        onTimeout=[on_timeout_handler],
        # Set a short timeout to demonstrate the onTimeout handler
        # For actual use, adjust or remove this timeout.
        timeout=5, 
        outputTimeout=2 # Kill if no output for 2 seconds
    )
    print(f"Executing command: {p.commandline}")
    p.run()
    return_code = p.wait()
    print(f"Process finished with return code: {return_code}")
    if p.timedOut:
        print("Note: Process was killed due to timeout.")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →