Python 在 flask 请求期间在 cwd 上弹出异常
Python popen exception on cwd during flask request
主要有两个步骤:
第一个是烧瓶端点:
@app.route('/updater/client', methods=['GET'])
def update_client():
try:
return _update(request, Unit.CLIENT)
except:
sv_exceptions.get_and_log_exception_info()
return Response(status=BAD_REQUEST)
_update()
方法是 'heavy'。它检查 github 存储库,区分两个修订版,形成一个 zip 存档并将其作为响应发回。可能需要一些时间。
问题是其中一个步骤是通过 popen 在存储库中找到最新修订:
def get_latest_version(unit: Unit):
repository_path = unit.repository_path
branch = unit.branch
print(f'repository_path: {repository_path}')
try:
with subprocess.Popen(["git", "rev-parse", branch],
cwd=r'{}'.format(repository_path),
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as p:
for line in p.stdout:
return line.rstrip('\r\n')
except Exception as e:
print(f'Exception in popen: {e}')
return None
当第一个请求在第二个请求之前完成时一切正常:
repository_path: ./../../../client-repository
127.0.0.1 - - [02/Sep/2021 13:04:44] "GET /updater/client?os=windows&version=e7369fc9 HTTP/1.1" 200 -
repository_path: ./../../../client-repository
127.0.0.1 - - [02/Sep/2021 13:04:46] "GET /updater/client?os=linux&version=e7369fc9 HTTP/1.1" 200 -
并且当其中一个请求在第二个请求完成之前开始时出现异常
repository_path: ./../../../client-repository
127.0.0.1 - - [02/Sep/2021 13:04:49] "GET /updater/client?os=windows&version=e7369fc9 HTTP/1.1" 400 -
repository_path: ./../../../client-repository
Exception in popen: [Errno 2] No such file or directory: './../../../client-repository'
expected str, bytes or os.PathLike object, not NoneType
127.0.0.1 - - [02/Sep/2021 13:04:50] "GET /updater/client?os=linux&version=e7369fc9 HTTP/1.1" 200 -
如您所见,repository_path
始终存在,但在第二种情况下,它与 No such file or directory
一起出现。
我不明白为什么会这样。
回答我自己的问题:
当我用绝对路径 /home/user/client-repository
替换相对路径 ./../../../client-repository
时有效。
我可能是错的,但我猜是因为并发性 Popen
尝试再次应用 cwd
时它已经在上一次执行的文件夹中。
主要有两个步骤:
第一个是烧瓶端点:
@app.route('/updater/client', methods=['GET'])
def update_client():
try:
return _update(request, Unit.CLIENT)
except:
sv_exceptions.get_and_log_exception_info()
return Response(status=BAD_REQUEST)
_update()
方法是 'heavy'。它检查 github 存储库,区分两个修订版,形成一个 zip 存档并将其作为响应发回。可能需要一些时间。
问题是其中一个步骤是通过 popen 在存储库中找到最新修订:
def get_latest_version(unit: Unit):
repository_path = unit.repository_path
branch = unit.branch
print(f'repository_path: {repository_path}')
try:
with subprocess.Popen(["git", "rev-parse", branch],
cwd=r'{}'.format(repository_path),
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as p:
for line in p.stdout:
return line.rstrip('\r\n')
except Exception as e:
print(f'Exception in popen: {e}')
return None
当第一个请求在第二个请求之前完成时一切正常:
repository_path: ./../../../client-repository
127.0.0.1 - - [02/Sep/2021 13:04:44] "GET /updater/client?os=windows&version=e7369fc9 HTTP/1.1" 200 -
repository_path: ./../../../client-repository
127.0.0.1 - - [02/Sep/2021 13:04:46] "GET /updater/client?os=linux&version=e7369fc9 HTTP/1.1" 200 -
并且当其中一个请求在第二个请求完成之前开始时出现异常
repository_path: ./../../../client-repository
127.0.0.1 - - [02/Sep/2021 13:04:49] "GET /updater/client?os=windows&version=e7369fc9 HTTP/1.1" 400 -
repository_path: ./../../../client-repository
Exception in popen: [Errno 2] No such file or directory: './../../../client-repository'
expected str, bytes or os.PathLike object, not NoneType
127.0.0.1 - - [02/Sep/2021 13:04:50] "GET /updater/client?os=linux&version=e7369fc9 HTTP/1.1" 200 -
如您所见,repository_path
始终存在,但在第二种情况下,它与 No such file or directory
一起出现。
我不明白为什么会这样。
回答我自己的问题:
当我用绝对路径 /home/user/client-repository
替换相对路径 ./../../../client-repository
时有效。
我可能是错的,但我猜是因为并发性 Popen
尝试再次应用 cwd
时它已经在上一次执行的文件夹中。