Mercurial > public > mercurial-scm > hg
comparison mercurial/utils/procutil.py @ 42496:ca1014ad3de4
procutil: allow callers of runbgcommand to assume the process starts
Experimentally starting the subprocess can take as much as 40ms, and
for some of our use cases that's frivolous: we know the binary will
start, and if it doesn't we'd only ever ignore it and continue
anyway. This lets those use cases be faster.
Differential Revision: https://phab.mercurial-scm.org/D6537
author | Augie Fackler <augie@google.com> |
---|---|
date | Tue, 18 Jun 2019 09:43:27 -0400 |
parents | b275dbb60089 |
children | f1f9ad5ae4f8 |
comparison
equal
deleted
inserted
replaced
42495:373aeede7352 | 42496:ca1014ad3de4 |
---|---|
468 # Following creation flags might create a console GUI window. | 468 # Following creation flags might create a console GUI window. |
469 # Using subprocess.CREATE_NEW_CONSOLE might helps. | 469 # Using subprocess.CREATE_NEW_CONSOLE might helps. |
470 # See https://phab.mercurial-scm.org/D1701 for discussion | 470 # See https://phab.mercurial-scm.org/D1701 for discussion |
471 _creationflags = DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP | 471 _creationflags = DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP |
472 | 472 |
473 def runbgcommand(script, env, shell=False, stdout=None, stderr=None): | 473 def runbgcommand( |
474 script, env, shell=False, stdout=None, stderr=None, ensurestart=True): | |
474 '''Spawn a command without waiting for it to finish.''' | 475 '''Spawn a command without waiting for it to finish.''' |
475 # we can't use close_fds *and* redirect stdin. I'm not sure that we | 476 # we can't use close_fds *and* redirect stdin. I'm not sure that we |
476 # need to because the detached process has no console connection. | 477 # need to because the detached process has no console connection. |
477 subprocess.Popen( | 478 subprocess.Popen( |
478 tonativestr(script), | 479 tonativestr(script), |
479 shell=shell, env=tonativeenv(env), close_fds=True, | 480 shell=shell, env=tonativeenv(env), close_fds=True, |
480 creationflags=_creationflags, stdout=stdout, | 481 creationflags=_creationflags, stdout=stdout, |
481 stderr=stderr) | 482 stderr=stderr) |
482 else: | 483 else: |
483 def runbgcommand(cmd, env, shell=False, stdout=None, stderr=None): | 484 def runbgcommand( |
485 cmd, env, shell=False, stdout=None, stderr=None, ensurestart=True): | |
484 '''Spawn a command without waiting for it to finish.''' | 486 '''Spawn a command without waiting for it to finish.''' |
485 # double-fork to completely detach from the parent process | 487 # double-fork to completely detach from the parent process |
486 # based on http://code.activestate.com/recipes/278731 | 488 # based on http://code.activestate.com/recipes/278731 |
487 pid = os.fork() | 489 pid = os.fork() |
488 if pid: | 490 if pid: |
491 if not ensurestart: | |
492 return | |
489 # Parent process | 493 # Parent process |
490 (_pid, status) = os.waitpid(pid, 0) | 494 (_pid, status) = os.waitpid(pid, 0) |
491 if os.WIFEXITED(status): | 495 if os.WIFEXITED(status): |
492 returncode = os.WEXITSTATUS(status) | 496 returncode = os.WEXITSTATUS(status) |
493 else: | 497 else: |