Skip to content

Add xw.apps.cleanup() to remove zombie processes #2001

@fzumstein

Description

@fzumstein
import xlwings as xw
from shlex import split
import subprocess

res = subprocess.run(
    split('tasklist /FI "IMAGENAME eq EXCEL.exe"'),
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    encoding="utf-8",
)

all_pids = set()
for line in res.stdout.splitlines()[3:]:
    # Automatically ignored if there's no processes as then it prints only 1 line
    _, pid, _, _, _, _ = line.split()
    all_pids.add(int(pid))

active_pids = {app.pid for app in xw.apps}
zombie_pids = all_pids - active_pids

for pid in zombie_pids:
    res = subprocess.run(
        split(f"taskkill /PID {pid} /F"),
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        encoding="utf-8",
    )

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions