我有python片段,它通过https访问令牌克隆了一个github存储库,创建了一个分支来修改内容,然后回退。如果直接在命令行上通过python解释器运行,则以下代码可以很好地工作。但是我有一个用例,其中我通过python的flask服务器以API形式公开了此功能。 Flask服务器通过python的“ gunicorn”框架运行,该框架是Python WSGI HTTP Server。现在,当我使用API调用时,在克隆存储库时会引发错误。我仅通过运行Flask服务器进行了测试,API调用工作得很好,并且在不引发异常的情况下可以正常工作。但是,当穿过金枪鱼时,同一瓶我却遇到了这个错误。
不确定如何消除该错误。
代码段:
import git
.........
.........
.........
try:
_repo = git.Repo.clone_from(
f"https://{_token}:x-oauth-basic@{self.git_url}", _repo_dir
)
_new_branch = _repo.create_head(_branch)
_repo.head.set_reference(_new_branch)
except Exception as e:
return False, str(e)
_update_path = os.path.join(
_repo_dir, f"repo1/config/"
)
if not os.path.exists(_update_path):
os.mkdir(_update_path)
with open(f"{_update_path}/users.json", "w+") as _fd:
json.dump(_users_json, _fd, indent=4)
_repo.git.add(A=True)
_repo.git.commit(m=_title)
_repo.git.push("origin", _branch)
错误:
2020-06-11 17:18:16,714 DEBUG: Popen(['git', 'clone', '-v', 'https://<ACCESS_TOKEN>:[email protected]/ganesh/repo1.git', '/tmp/folder-1234_1591895896.185123'], cwd=/opt/ganesh, universal_newlines=True, shell=None, istream=None)
2020-06-11 17:18:16,739 DEBUG: Cmd(['git', 'clone', '-v', 'https://<ACCESS_TOKEN>:[email protected]/ganesh/repo1.git', '/tmp/folder-1234_1591895896.185123'])'s unused stdout: Cloning into '/tmp/folder-1234_1591895896.185123'...
2020-06-11 17:18:16,740 DEBUG: AutoInterrupt wait stderr: b'Error reading command stream\n'
感谢任何帮助。谢谢
此问题已解决。 gunicorn发生了这种情况,因为它通过创建多个工作进程以守护模式运行flask服务器。 gitpython代码库,
期望某个流读取其每个输出。但是在守护进程的情况下,没有输出流侦听器,并且clone_from模块引发异常。
我通过在可食用玉米粒侧设置一些选项来捕获这些流来解决了这个问题。
--enable-stdio-inheritance \
--error-logfile /tmp/gunicorn_error.log \
--capture-output \
关于缺少输出流阅读器的情况为何会表现出此行为的问题,在gitpython存储库中已引发此问题。
https://github.com/gitpython-developers/GitPython/issues/1020