#๐Ÿ”’ How to clean up timeout-ed subprocess.run process?

16 messages ยท Page 1 of 1 (latest)

native lodge
#

I have the following snippet to run a task and terminate it if a timeout is exceeded.

    try:
        process = subprocess.run(
            [
                node_path,
                render_puppeteer_path,
                task.url,
                tempdir,
            ],
            capture_output=True,
            check=False,
            text=True,
            timeout=TIMEOUT,
        )
    except subprocess.TimeoutExpired:
        delete_dir(tempdir)
        return Box(
            success=False,
            task=task,
         data=dict(reason='timeout'),
        )

My problem is that children of the running process is not cleared up after a timeout. How can I modify it such that they are cleared up?

digital fieldBOT
#

@native lodge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wanton marlin
#

Otherwise you can kill the extra subprocesses with psutil

native lodge
#

Yes, it's a node script starting Puppeteer which starts Chrome. My problem is that the Chrome processes stay running after the Python timeout.

#

How can I kill the subprocesses? Do I have a process id or similar in the except subprocess.TimeoutExpired block?

wanton marlin
#

Eg psutil.Process().children()

native lodge
#

somehow it doesn't end up there

#

in pstree they are in a higher level

wanton marlin
#

Where do they end up?

#

Oh it goes to init

#

You might have to fix this in the node process

native lodge
#

Yes, it goes to init. I'll fix it with a pkill.

digital fieldBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.