python - 为什么我的 post-receive Hook 在从子进程管道读取时挂起?

标签 python git subprocess pipe

我正在 Git 存储库上运行 Gitolite,并且那里有 post-receive Hook 。这个钩子(Hook)的脚本是用Python编写的,并且在之后失败

proc = subprocess.Popen('git log', shell = True, stdout=subprocess.PIPE)
out = proc.stdout.read()

它不会在这些行之后执行。如果我手动运行这个脚本,它会完美运行。

我做错了什么?

最佳答案

来自subprocess documentation :

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/

相关文章:

python - 无法使用 python 和 pyserial 打开/dev/ttyusb0

android - 更改后如何同步android repo?

python - 使用Python C API时如何中断Windows上的Python子进程?

Python 子进程 (ffmpeg) 仅在我按 Ctrl-C 程序时启动?

python - 安装 ocropus-0.4.4

Python Mechanize 表单提交

git - 相当于 future 贡献者 pull 的 "git update-index --skip-worktree <file>"吗?

Git:当完全相同的冲突再次发生时,我可以 "cherry-pick" merge 冲突解决方案吗?

python - 如何解析子流程的结果

python - CentOS 在随机时间运行 python 脚本