0
 #####################################
 # Command code
 ####################################
import shlex
import subprocess


class CommandError(Exception):
   pass


class Command(object):
"""
Class to simplify the process of running a command and getting the output
and return code
"""
    def __init__(self, command=None):
       self.command = command
       self.args = None
       self.subprocess = None
       self.output = None
       self.errors = None
       self.status = None
       self.result = None

   def run(self, command=None):
       if command is not None:
           self.command = command
       if self.command is None:
           raise CommandError('No command specified')
       self.args = shlex.split(self.command)
       self.subprocess = subprocess.Popen(self.args,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
       self.output, self.errors = self.subprocess.communicate()
       self.subprocess.wait(60)
       self.status = self.subprocess.returncode
       self.result = (0 == self.status)
       return self.result

   def A(X, logger,device_name,ip_address,value_type, value):
       command_string = "my_command" 
       cmd = Command(command_string)
       time.sleep(1.0)
       if not cmd.run():
           logger.error("cmd.errors = %s", device_name, cmd.errors.rstrip())
       elif 'No Such Instance' in cmd.output:
           logger.error("cmd.output = %s", device_name, cmd.output.rstrip())
       else:
            result = True
            return result

I added a self.subprocess.wait(60) for 60 seconds for my commands to run with a timeout value specified.

But it is throwing the following error ::self.subprocess.wait(60) TypeError: wait() takes exactly 1 argument (2 given)

What is the problem with the code? Can anyone point out the issue?

1 Answers1

0

You have to change the code inside class Command, maybe Command.run(), to do its job without blocking the script execution. There is no good way to do what you ask, without knowing exactly what that class does.

The solutions (Threads, Processes, Asynchronous code) all depend on the specifics of your blocking code and can't be generally recommended with certain.

EDIT: Now that you added the Command class, we can examine which part is blocking. In your code the part that blocks is this line:

self.subprocess.wait()

Luckly that function has a timeout parameter as you can see in the subprocess module documentation.

self.subprocess.wait(60)

That will automatically wait for 60 seconds and throw an error if it fails, as you asked.

EDIT 2:

The error you're now getting means your version of python doesn't have the timeout parameter. Which version of python are you using? As you can see in the documentation linked above, you need at least python 3.3 to use it.

nosklo
  • 217,122
  • 57
  • 293
  • 297
  • I added the self.subprocess.wait(60) .. Now I'm facing the error as follows TypeError: wait() takes exactly 1 argument (2 given). How do I solve this? Can you paste the code for me? – Harish Sridharan Feb 27 '18 at 19:40
  • Did you read the documentation? The parameter is there since python 3.3 are you using it? The code is correct and works fine in my tests. – nosklo Mar 01 '18 at 12:23