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 8270189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:30:35+00:00 2026-06-08T06:30:35+00:00

I searchedon internet and learned other method of implementing it the problem now i

  • 0

I searchedon internet and learned other method of implementing it
the problem now i have found out. my execution time always becomes more than that
of time out if i write stdout=subprocess.PIPE in subprocess.Popen. If i am removing it then it is taking normal execution time

import subprocess, datetime, os, time, signal
//setting time for timeout
timeout=3
start = datetime.datetime.now()
process = subprocess.Popen(["python", "/home/bourne/untitled.py"],shell=False, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
while process.poll() is None:
        time.sleep(0.1)
        now = datetime.datetime.now()
        if (now - start).seconds > timeout:
            os.kill(process.pid, signal.SIGKILL)
            os.waitpid(-1, os.WNOHANG)
            print "error"       
            print (now - start).seconds
            break
        print (now - start).seconds
  • 1 1 Answer
  • 0 Views
  • 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-06-08T06:30:40+00:00Added an answer on June 8, 2026 at 6:30 am
    I have successfully solved the problem. the solution is 
    
    import subprocess, signal, os, threading, errno
    from contextlib import contextmanager
    
    class TimeoutThread(object):
        def __init__(self, seconds):
            self.seconds = seconds
            self.cond = threading.Condition()
            self.cancelled = False
            self.thread = threading.Thread(target=self._wait)
    
        def run(self):
            """Begin the timeout."""
            self.thread.start()
    
        def _wait(self):
            with self.cond:
                self.cond.wait(self.seconds)
    
                if not self.cancelled:
                    self.timed_out()
    
        def cancel(self):
            """Cancel the timeout, if it hasn't yet occured."""
            with self.cond:
                self.cancelled = True
                self.cond.notify()
            self.thread.join()
    
        def timed_out(self):
            """The timeout has expired."""
            raise NotImplementedError
    
    class KillProcessThread(TimeoutThread):
        def __init__(self, seconds, pid):
            super(KillProcessThread, self).__init__(seconds)
            self.pid = pid
    
        def timed_out(self):
            try:
                os.kill(self.pid, signal.SIGKILL) // this is for linux you need to change it for windows
            except OSError,e:
                # If the process is already gone, ignore the error.
                if e.errno not in (errno.EPERM, errno. ESRCH):
                    raise e
    
    @contextmanager
    def processTimeout(seconds, pid):
        timeout = KillProcessThread(seconds, pid)
        timeout.run()
        try:
            yield
        finally:
            timeout.cancel()
    
    
    def example(cmd):
        proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        //setting the timeout to be 1 sec
        with processTimeout(1, proc.pid):
            stdout,stderr=proc.communicate()       
    
        resultcode = proc.wait()
        if resultcode < 0:
            #print "error: %i" % resultcode
            return resultcode,0
        else:
            return stdout,stderr
    
    
    
    
    
    //This is used to create new subprocess and it will return output as well as error
    output,err=example(["python",filepath,"5"])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched through internet & found that there are no any direct method
I've searched the Internet and have found some good solutions for teeing STDOUT to
I have searched internet and stackoverflow thoroughly, but I haven't found answer to my
Ive searched the internet, but found nothing pertaining to my question. From the link
I have searched the internet and the perforce help pages, but couldn't find a
I have searched the internet for an answer to what could be causing EXCEPTION_ACCESS_VIOLATION
I have a problem with MDS-CS simulator coming with JDE 4.5.0 installation. I have
I came to do the eternal question, which so far have not found a
I faced this question in interview. When I searched on internet I found different
Okay, I have searched on the internet for answers- sadly to no avail. I'm

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.