我正在 Git 存储库上运行 Gitolite,并且那里有 post-receive Hook 。这个钩子(Hook)的脚本是用Python编写的,并且在之后失败
proc = subprocess.Popen('git log', shell = True, stdout=subprocess.PIPE)
out = proc.stdout.read()
它不会在这些行之后执行。如果我手动运行这个脚本,它会完美运行。
我做错了什么?
最佳答案
Warning
Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.
如果可能的话,我也会避免使用 shell=True
(IMO,它仅在您想使用 shell 内置函数并且它是特定于平台/shell 的情况下才有用),并将 Popen 命令作为列表传递,就像['git', 'log']
尝试如下:
>>> proc = subprocess.Popen(['git', 'log'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> proc.communicate()
('', 'fatal: Not a git repository (or any of the parent directories): .git\n')
communicate()[0]
是标准输出,communicate()[1]
是标准错误。
关于python - 为什么我的 post-receive Hook 在从子进程管道读取时挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10916659/