Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fzumstein opened this issue Aug 26, 2022 · 2 comments · Fixed by #2191
Closed

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

fzumstein opened this issue Aug 26, 2022 · 2 comments · Fixed by #2191
Milestone

Comments

@fzumstein
Copy link
Member

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",
    )
@bravegag
Copy link

@fzumstein thanks a lot for this! I have couple of questions:

  1. should we run this at the beginning or at the end?
  2. will the 2x python xlwings processes per excel will also be killed by this?

Cheers from Schwyz

@fzumstein
Copy link
Member Author

  • It should probably run during initialization and via atexit handler. The first one to clean up zombie processes caused by a crash in a previous python process and the latter one to clean up during an ordinary shut down. I'll need to test if it's also a better solution for the App exit: https://github.com/xlwings/xlwings/blob/main/xlwings/main.py#L774
  • Not sure what you mean by 2, but please open a separate issue for this one.

@fzumstein fzumstein changed the title Add xw.apps.kill_zombies() Add xw.apps.cleanup() to remove zombie processes Mar 15, 2023
@fzumstein fzumstein added this to the 0.30.2 milestone Mar 15, 2023
@github-project-automation github-project-automation bot moved this from TODO to Done in xlwings planner Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants