Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1112783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:45:42+00:00 2026-05-17T02:45:42+00:00

I realize this might be a duplicate of Using module 'subprocess' with timeout .

  • 0

I realize this might be a duplicate of Using module 'subprocess' with timeout. If it is, I apologize, just wanted to clarify something.

I’m creating a subprocess, which I want to run for a certain amount of time, and if it doesn’t complete within that time, I want it to throw an error. Would something along the lines of the following code work or do we have to use a signal like answered in the other question? Thanks in advance!:

def run(self):
    self.runTestCmd()
    self.waitTestComplete(self.timeout)

def runTestCmd(self):
    self.proc = subprocess.Popen("./configure", shell=True)

def waitTestComplete(self, timeout):
    st = time.time() 
    while (time.time()-st) < timeout:
        if self.proc.poll() == 0:
            return True
        else:
            time.sleep(2)
    raise TestError("timed out waiting for test to complete")
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:45:43+00:00Added an answer on May 17, 2026 at 2:45 am

    It would, but it has a problem. The process will continue on doing whatever it is you asked it to do even after you’ve given up on it. You’ll have to send the process a signal to kill it once you’ve given up on it if you really want it to stop.

    Since you are spawning a new process (./configure which is presumably a configure script) that in turn creates a whole ton of sub-processes this is going to get a little more complex.

    import os
    
    def runTestCmd(self):
        self.proc = subprocess.Popen(["./configure"], shell=False,
                                     preexec_fn=os.setsid)
    

    Then os.kill(-process.pid, signal.SIGKILL) should kill all the sub-processes. Basically what you are doing is using the preexec_fn to cause your new subprocess to acquire it’s own session group. Then you are sending a signal to all processes in that session group.

    Many processes that spawn subprocesses know that they need to clean up their subprocesses before they die. So it behooves you to try being nice to them if you can. Try os.signal(-process.pid, signal.SIGTERM) first, wait a second or two for the process to exit, then try SIGKILL. Something like this:

    import time, os, errno, signal
    
    def waitTestComplete(self, timeout):
        st = time.time() 
        while (time.time()-st) < timeout:
            if self.proc.poll() is not None:  # 0 just means successful exit
                # Only return True if process exited successfully,
                # otherwise return False.
                return self.proc.returncode == 0
            else:
                time.sleep(2)
        # The process may exit between the time we check and the
        # time we send the signal.
        try:
            os.kill(-self.proc.pid, signal.SIGTERM)
        except OSError, e:
            if e.errno != errno.ESRCH:
                # If it's not because the process no longer exists,
                # something weird is wrong.
                raise
        time.sleep(1)
        if self.proc.poll() is None: # Still hasn't exited.
            try:
                os.kill(-self.proc.pid, signal.SIGKILL)
            except OSError, e:
                if e.errno != errno.ESRCH:
                    raise
        raise TestError("timed out waiting for test to complete")
    

    As a side note, never, ever use shell=True unless you know for absolute certain that’s what you want. Seriously. shell=True is downright dangerous and the source of many security issues and mysterious behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greets. I realize this might be seen as a duplicate question as this but
I realize that this might be a duplicate question but this question is very
I realise this might be hard to explain, so let me start by using
I realize this is a duplicate of about 20 different posts, but none of
I realize this might be a long shot, but does anyone have an example
I realize this might not be a best fit for SO, so please tell
I realize this question might not be that programming related, and that it by
I realize this might not be the best setup, but humor me, is it
Upon pre-submission review, I realize this question might be incredibly silly. if (this.gameOver(gpos) >
I realize that this question might not make sense to some, but I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.