【问题标题】:Tmpfile error with django-import-export on HerokuHeroku 上的 django-import-export 出现 Tmpfile 错误
【发布时间】:2017-07-16 00:11:10
【问题描述】:

我正在使用django-import-export 处理上传到我的 Django 管理站点的 CSV 文件。

当我在本地机器上运行 Django 时,一切正常。

当我将应用程序部署到 Heroku 时,我开始收到与 tmpfile 访问相关的错误:

Feb 24 14:35:12 test-staging app/web.3:  ERROR 2017-02-24 22:35:12,143 base 14 139687073408832 Internal Server Error: 
....
Feb 24 14:35:12 test-staging app/web.3:    File "/app/.heroku/python/lib/python2.7/site-packages/import_export/admin.py", line 163, in process_import 
Feb 24 14:35:12 test-staging app/web.3:      data = tmp_storage.read(input_format.get_read_mode()) 
Feb 24 14:35:12 test-staging app/web.3:    File "/app/.heroku/python/lib/python2.7/site-packages/import_export/tmp_storages.py", line 42, in read 
Feb 24 14:35:12 test-staging app/web.3:      with self.open(mode=mode) as file: 
Feb 24 14:35:12 test-staging app/web.3:    File "/app/.heroku/python/lib/python2.7/site-packages/import_export/tmp_storages.py", line 31, in open 
Feb 24 14:35:12 test-staging app/web.3:      return open(self.get_full_path(), mode) 
Feb 24 14:35:12 test-staging app/web.3:  IOError: [Errno 2] No such file or directory: u'/tmp/tmpvCUtrP' 

我已经阅读了有关 Heroku 临时存储的信息,看来这应该可行。我已经验证我可以通过 heroku run 使用我的代码在 heroku dyno 上的 /tmp 中创建、查看和修改文件。

django-import-export 有一个模块,允许您重载临时文件创建机制 - 但我什至不确定这里出了什么问题(或者,更确切地说,为什么 /tmp/tmpvCUtrP 没有被创建或没有被创建可见)。

【问题讨论】:

    标签: python django heroku django-import-export


    【解决方案1】:

    django-import-export 将导入过程拆分为几个对服务器的请求。

    Heroku 可以使用多个 dyno,它们不共享 /tmp 中的公共文件系统。

    偶然一个初始请求可以做 dyno A 并在 /tmp/tmpfilename 中创建 tmpfile,然后稍后的请求可以转到 dyno B,并且 django-import-export 需要 /tmp/tmpfilename 存在 - 但是不是。

    为了解决这个问题,我切换到了 django-import-export 的 CacheStorage 临时存储方法:

    from import_export.tmp_storages import CacheStorage
    
    class CustomAdminForm(ImportExportModelAdmin):
        tmp_storage_class = CacheStorage
    

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2021-11-14
      • 1970-01-01
      • 2020-12-09
      • 2015-05-08
      相关资源
      最近更新 更多