0

I'm writing a Python script that will refresh a Git repository, then build it.
The script is to be run daily (e.g. running on a daily build server).

The issue is that git commands to our server sometimes never complete (ok, I terminate the processes after 10 minutes or more).

I would like to set a Timeout for the subprocess (and probably put into a retry loop).
How do I set up the Timeout for a subprocess.

Here's my code so far:

#------------------------------------------------------------------------------
#   Script to daily build Voyant 3
#------------------------------------------------------------------------------
import subprocess
import os

#------------------------------------------------------------------------------
#   Main program starts here.
#------------------------------------------------------------------------------
repo_dir = "c:/sandboxes/git/voyant-3"

#------------------------------------------------------------------------------
#   Change to repo (repository) folder.
#------------------------------------------------------------------------------
os.chdir(repo_dir);

#------------------------------------------------------------------------------
#   Refresh the repo (sandbox)
#------------------------------------------------------------------------------
subprocess.run(["git", "fetch", "--all]"]);

Notes:

  1. Using Python 27 (preferred) or Python 36
  2. Using Windows 10 (The script will be registered as a service on the PC)
  3. Research on using PyGit is not impressive; most Users say to use Python Subprocess.
  4. From the Python documentation: (https://docs.python.org/3/library/subprocess.html):

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)

2
  • 2
    set timeout parameter and catch the TimeoutExpired error? Commented Jul 7, 2021 at 3:26
  • 2
    As an alternative, you could use the timeout command, which can also do the "TERM, then KILL" sequence; subprocess.run(["timeout", "-k30", "600", "git", "fetch", "--all"]) Commented Jul 7, 2021 at 7:22

1 Answer 1

3
while True:
    try: subprocess.run(["git", "fetch", "--all"], timeout=600);
    except subprocess.TimeoutExpired: continue
    break
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.