SoFunction
Updated on 2024-10-30

Example of how Python sets a timeout for command execution

preamble

Several libraries or functions used to execute command lines cannot set a timeout for the execution of a command, which is used to terminate the execution of the command when the command execution timeout is exceeded, but this feature has been solved in (?). This function has been solved in (?), but it can only be realized by yourself. The following words without further ado, to look at the details of the introduction.

A version is simply implemented below:

import subprocess
from threading import Timer


def call(args, timeout):
 p = (args, stdout=, stderr=)

 timer = Timer(timeout, lambda process: (), [p])

 try:
  ()
  stdout, stderr = ()
  return_code = 
  return (stdout, stderr, return_code)
 finally:
  ()

beta (software)

print call(['hostname'], 2)
print call(['ping', ''], 2)

summarize

Above is the entire content of this article, there are many shortcomings in this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for your support.