无法联系“工人”来自 Web 进程的 Heroku 进程:`ConnectionRefusedError`

发布于 2025-01-10 19:46:08 字数 1659 浏览 0 评论 0原文

我有一个 web 进程和一个 api 进程(一个类似于 文档这些其他文档)。但是,我不明白不同进程之间的网络是如何工作的。

两个进程都在侦听 0.0.0.0,并且工作进程绑定到端口 5000。但是,当我尝试从 web 进程发出请求时,我得到一个ConnectionRefusedError

ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=5000): Max retries exceeded with url: /tokenize?sentence=hello (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd307019dc0>: Failed to establish a new connection: [Errno 111] Connection refused'))

我应该找出其他进程的IP吗?我在这里做错了什么吗?

Procfile:

api: python app.py
web: voila UI.ipynb --port $PORT --Voila.ip=0.0.0.0

app.py:

from flask import Flask, request
from ie_utils import tokenize
app = Flask(__name__)

@app.route("/")
def home():
    return {
        "message": "Hello world!",
        "version": "0.1",
    }


if __name__ == "__main__":
    import os
    port = 5000
    app.run(host="0.0.0.0", port=port)

UI.ipynb中的相关代码:

import requests
requests.get("http://0.0.0.0:5000/")

完整源代码:https://github.com/astrojuanlu/ie-titanic-utils-a/tree/test-procfile

I have a web process and an api process (a "worker" process similar to what is described in the docs and these other docs). However, I don't understand how the networking between different processes works.

Both processes are listening on 0.0.0.0, and the worker process is bound to port 5000. However, when I try to make a request from the web process, I get a ConnectionRefusedError:

ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=5000): Max retries exceeded with url: /tokenize?sentence=hello (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd307019dc0>: Failed to establish a new connection: [Errno 111] Connection refused'))

Am I supposed to figure out the IP of the other process? Am I doing something wrong here?

Procfile:

api: python app.py
web: voila UI.ipynb --port $PORT --Voila.ip=0.0.0.0

app.py:

from flask import Flask, request
from ie_utils import tokenize
app = Flask(__name__)

@app.route("/")
def home():
    return {
        "message": "Hello world!",
        "version": "0.1",
    }


if __name__ == "__main__":
    import os
    port = 5000
    app.run(host="0.0.0.0", port=port)

Relevant code in UI.ipynb:

import requests
requests.get("http://0.0.0.0:5000/")

Full source code: https://github.com/astrojuanlu/ie-titanic-utils-a/tree/test-procfile

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2025-01-17 19:46:08

我不明白不同进程之间的网络是如何工作的

除非您使用 私人空间 (部分Heroku 的企业产品),事实并非如此

通用运行时通过防火墙将所有测功机彼此隔离,提供强大的隔离。可以到达 dyno 的唯一流量是从路由器转发到侦听 $PORT 环境变量中指定的端口号的 Web 进程的 Web 请求。 Worker 和一次性 dyno 无法接收入站请求。

我不完全清楚你想在这里做什么,但你无法通过向本地主机发出 HTTP 请求来做到这一点。您可能会更幸运地将其作为两个单独的应用程序运行 - 在这种情况下,您的 API 可能是它自己的 web 进程,并且您的 Voilà 应用程序可以向 API 应用程序的主机名发出请求。

旁注:即使在允许这样做的系统上,您的请求也不应该发送至 0.0.0.0。那不是真实的 IP 地址。

告诉 Flask 绑定到 0.0.0.0 实际上意味着它应该监听所有可用的 IP 地址。当您想要发出请求时,您应该使用系统正在使用的真实主机名或 IP 地址之一,例如 localhost127.0.0.1 或其公共 IP 之一地址或主机名。

I don't understand how the networking between different processes works

Unless you are using Private Spaces (part of Heroku's enterprise offering), it doesn't:

The Common Runtime provides strong isolation by firewalling all dynos off from one another. The only traffic that can reach a dyno is web requests forwarded from the router to web processes listening on the port number specified in the $PORT environment variable. Worker and one-off dynos cannot receive inbound requests.

I'm not totally clear what you're trying to do here, but you won't be able to do it by making an HTTP request to localhost. You might have better luck running this as two separate apps—in that case your API could be its own web process and your Voilà app could make requests to the API app's hostname.

Side note: even on a system where this is permitted your request should not go to 0.0.0.0. That isn't a real IP address.

Telling Flask to bind to 0.0.0.0 really means that it should listen on all available IP addresses. When you want to make a request you should use one of the real hostnames or IP addresses that your system is using, e.g. localhost or 127.0.0.1 or one of its public IP addresses or hostnames.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文