Skip to content

Instantly share code, notes, and snippets.

@kirpit kirpit/bash.py
Last active Aug 13, 2019

Embed
What would you like to do?
Enables to run subprocess commands in a different thread with TIMEOUT option!
#! /usr/bin/env python
import threading
import subprocess
import traceback
import shlex
class Command(object):
"""
Enables to run subprocess commands in a different thread with TIMEOUT option.
Based on jcollado's solution:
http://stackoverflow.com/questions/1191374/subprocess-with-timeout/4825933#4825933
"""
command = None
process = None
status = None
output, error = '', ''
def __init__(self, command):
if isinstance(command, basestring):
command = shlex.split(command)
self.command = command
def run(self, timeout=None, **kwargs):
""" Run a command then return: (status, output, error). """
def target(**kwargs):
try:
self.process = subprocess.Popen(self.command, **kwargs)
self.output, self.error = self.process.communicate()
self.status = self.process.returncode
except:
self.error = traceback.format_exc()
self.status = -1
# default stdout and stderr
if 'stdout' not in kwargs:
kwargs['stdout'] = subprocess.PIPE
if 'stderr' not in kwargs:
kwargs['stderr'] = subprocess.PIPE
# thread
thread = threading.Thread(target=target, kwargs=kwargs)
thread.start()
thread.join(timeout)
if thread.is_alive():
self.process.terminate()
thread.join()
return self.status, self.output, self.error
@alevchuk

This comment has been minimized.

Copy link

alevchuk commented May 16, 2012

I get:

Traceback (most recent call last):
File "./subprocess_timeout.py", line 34, in
command.run(timeout=3)
File "./subprocess_timeout.py", line 31, in run
return self.process.returncode
AttributeError: 'NoneType' object has no attribute 'returncode'

@ogasser

This comment has been minimized.

Copy link

ogasser commented Aug 31, 2012

Pass shell=True if you want Popen to execute the program in a shell. Beware of maclicious input though!

Example:

command = Command("echo 'Process started'; sleep 2; echo 'Process finished'")
command.run(timeout=1, shell=True)
@pengqi

This comment has been minimized.

Copy link

pengqi commented Sep 6, 2012

command = Command('ping www.redicecn.com -t')
print command.run(timeout=1, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True)

Not work on Window7, Python 2.7.

@kirpit

This comment has been minimized.

Copy link
Owner Author

kirpit commented Dec 29, 2012

all these complains should be satisfied now with the latest revision.

it should also be able to run commands even though no shell=True argument provided, which is done by shlex splitting..

@davidyoung8906

This comment has been minimized.

Copy link

davidyoung8906 commented Jun 8, 2014

I use the cmd to start a java program. I find that the thread will keep running after thread.join()

@sunway1988

This comment has been minimized.

Copy link

sunway1988 commented Apr 27, 2016

Now we can use timeout in Ubuntu 10.04 and later versions happily.

@onkar27

This comment has been minimized.

Copy link

onkar27 commented Jul 3, 2017

Solution.java is

class Solution{
      public static void main(String [] args){ 
              do
                    System.out.println("Helloo");
              while(true);
      }
}

and

command = Command("java Solution")
print command.run(timeout=1, shell=True ,stderr=subprocess.STDOUT, stdout=subprocess.PIPE)

Timeout does not works !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.