在heroku dyno中从节点到python进行http调用 [英] Make http call from node to python in heroku dyno
问题描述
我继承了一个包含NodeJS部分和Python部分的存储库。项目结构是这样的:NodeJS位于存储库的根目录,Python片段位于Python文件夹中:
I've inherited a repository that contains both a NodeJS part and a Python part. The project structure is such that the NodeJS is at the root of the repository, and the Python pieces are in a Python folder:
root
|- app
|- some.js
|- files.js
|- Python
|- other.py
|- files.py
我已经设置了一个Heroku dyno来为Python服务器和NodeJS服务器提供服务,在我的Procfile中执行此操作(基于这篇较旧的文章):
I've set up a Heroku dyno to serve both a Python server and a NodeJS server, by doing this in my Procfile (based loosely on this older article):
web: npm start
python: sh -c 'cd ./Python/ && export PYTHONPATH=. && python other.py'
python项目设置为使用端口2001.它是一个 Bottle 应用程序以及启动服务器时,我通过此端口(取自Heroku设置)。
The python project is set up to use port 2001. It's a Bottle application and on starting the server I pass in this port (taken from the Heroku settings).
在我的日志记录中,我可以看到两者都成功启动。简化:
In my logging, I can see that both are started successfully. Simplified:
Bottle v0.12.13 server starting up (using WaitressServer())...
Listening on http://localhost:2001/
Server running at: http://<guid>:7667
当我请求我的应用程序的根时,我收到来自NodeJS服务器的回复。但是,对于某些路由,我想调用Python部分。我通过使用Node的 http 在本地成功完成了此操作。但是,在Heroku上执行此操作时,我收到以下错误:
When I request the root of my application, I get a reply from the NodeJS server. However, for some routes, I want to make a call to the Python part. I've succesfully done this locally, by using Node's http. However, when doing this on Heroku, I receive the following error:
connect ECONNREFUSED 127.0.0.1:2001
当我在Heroku中本地运行此项目时(通过CLI工具 heroku local $ c $ ),它完美的作品。任何想法,我怎么能得到这个工作?
When I run this project locally in Heroku (via the CLI tool heroku local
), it works perfectly. Any ideas on how I can get this to work?
推荐答案
终于找到了它。 Dynos无法通过HTTP相互通信,只能通过队列进行通信。我无法再在文档中找到它,但他们在某处说明它。
Finally found it. Dynos can't communicate with each other via HTTP, only via queues. I can't find it in the docs anymore, but they state it somewhere.
因此,我将Procfile更改为只运行一个dyno:
So I changed my Procfile to only run one dyno:
web: npm start & sh -c 'cd ./Python/ && export PYTHONPATH=. && python other.py'
现在我可以打电话给 127.0.0.1 :2001
没有任何问题。
Now I can make my call to 127.0.0.1:2001
without any problems.
这篇关于在heroku dyno中从节点到python进行http调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!