Google App Engine Flexible 上的 Django:创建和删除文件
Django on Google App Engine Flexible: create and remove files
我在 Google App Engine(灵活环境)上部署了一个 Django 应用程序。
我可以像这样创建文件和删除文件吗:
with open('myfile.xls', 'w') as file_object:
#writing to file
os.remove('myfile.xls')
或使用 NamedTemporaryFile?
现在这段代码可以工作,但是在部署我的应用程序几天后变得 运行 缓慢。这与冷启动无关。重新部署修复它。 Сan文件不被删除浪费磁盘space?还是另有原因?
即使在标准环境中,也不推荐这样做。 Std 中的 Pyhton 提供了写入文件的位置 /tmp
,但鉴于 App Engine 会根据需要进行扩展,因此无法保证稍后文件仍会存在:
The runtime includes a full filesystem. The filesystem is read-only except for the location /tmp, which is a virtual disk storing data in your App Engine instance's RAM.
在 Organizing COnfiguration Files
中有一个关于 Design considerations for instance uptime
的部分,其中提到:
Your app should be "stateless" so that nothing is stored on the instance.
你应该使用 Google Cloud Storage instead. Here you can find an example of Python Google Cloud Storage sample for Google App Engine Flexible Environment.
我在 Google App Engine(灵活环境)上部署了一个 Django 应用程序。
我可以像这样创建文件和删除文件吗:
with open('myfile.xls', 'w') as file_object:
#writing to file
os.remove('myfile.xls')
或使用 NamedTemporaryFile?
现在这段代码可以工作,但是在部署我的应用程序几天后变得 运行 缓慢。这与冷启动无关。重新部署修复它。 Сan文件不被删除浪费磁盘space?还是另有原因?
即使在标准环境中,也不推荐这样做。 Std 中的 Pyhton 提供了写入文件的位置 /tmp
,但鉴于 App Engine 会根据需要进行扩展,因此无法保证稍后文件仍会存在:
The runtime includes a full filesystem. The filesystem is read-only except for the location /tmp, which is a virtual disk storing data in your App Engine instance's RAM.
在 Organizing COnfiguration Files
中有一个关于 Design considerations for instance uptime
的部分,其中提到:
Your app should be "stateless" so that nothing is stored on the instance.
你应该使用 Google Cloud Storage instead. Here you can find an example of Python Google Cloud Storage sample for Google App Engine Flexible Environment.