Skip to main content
 首页 » 编程设计

python之部署 Python Web 应用程序后出现 App Engine 错误之BlockingIOError : [Errno 11] Resource temporarily unavailable

2024年11月24日37xxx_UU

我在 Python 3.7 的标准环境中运行我的网络应用程序,当我在本地测试它时一切正常。但是,在我部署我的应用程序后,当该应用程序尝试将文件保存到我指定的 /tmp 位置时,我收到以下错误:

BlockingIOError: [Errno 11] Resource temporarily unavailable 

这是指定保存路径和文件名的方式。

file_num = os.urandom(10).hex() 
 
handle, path = tempfile.mkstemp() 
 
ytdl_format_options = {'format': 'bestaudio/best','outtmpl':path + 'song'+ file_num +'.mp3', 'quiet': True} 

我不确定是什么导致了这个错误,或者我是否在 app.yaml 或 gunicorn 上缺少某种类型的配置文件设置。我还尝试在 glcoud shell 上找到 gunicorn 文件,但它们没有出现在目录中。

我注意到,当我提交对文件的新更改并重新部署应用程序时,我的应用程序会随机正常运行,但它只会运行一次,然后如果我立即尝试再次运行该函数,它就会失败。这是我收到的错误:

Traceback (most recent call last): 
  File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 279, in handle 
    keepalive = self.handle_request(req, conn) 
  File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 348, in handle_request 
    six.reraise(*sys.exc_info()) 
  File "/env/lib/python3.7/site-packages/gunicorn/six.py", line 625, in reraise 
    raise value 
  File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 331, in handle_request 
    resp.write_file(respiter) 
  File "/env/lib/python3.7/site-packages/gunicorn/http/wsgi.py", line 403, in write_file 
    if not self.sendfile(respiter): 
  File "/env/lib/python3.7/site-packages/gunicorn/http/wsgi.py", line 393, in sendfile 
    sent += sendfile(sockno, fileno, offset + sent, count) 
BlockingIOError: [Errno 11] Resource temporarily unavailable 

请您参考如下方法:

来自 Filesystem :

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.

所以:

  • 使用 /tmp 以外的任何位置都不起作用
  • 可用空间有限。因此,您的应用需要注意在使用后删除临时文件。您描述的行为表明可用空间可能只够您的 1 个文件使用。

要考虑的另一件事是多个并行请求:如果每个这样的请求都可以创建自己的文件,那么可能会出现错误,因为多个文件将由不同的此类请求同时写入。添加一个限制并行处理的请求数量的方案是一种可能的方法。

虽然不是 100% 确定,但我怀疑限制可能也取决于实例的 RAM 总量,在这种情况下,您的应用配置为 instance class也很重要。要检查这是否属实,只需临时配置一个具有更多 RAM 的实例类,看看是否有帮助(例如,如果不清理文件,它可能会在遇到错误之前工作不止一次)。

如果上述怀疑得到证实,那么选择合适的实例类也可能是解决问题的一种途径。

尽管应用了上述所有方法,但仍有可能遇到问题,例如,如果您正在编写的某些文件太大。如果是这样,那么使用本地文件系统不适合您的应用程序。一个可能的替代方案是 store your files in Google Cloud Storage (GCS)相反。