如何在 Heroku 中的 Python 服务器上执行 "g++" 命令?

How to execute a "g++" command on a Python server in Heroku?

本文关键字:执行 g++ 命令 服务器 Heroku 中的 Python      更新时间:2023-10-16

我正在尝试将Python服务器部署到Heroku,我需要在其中一个库上执行"g ++"命令才能将其安装在服务器上。

我想创建一个 gunicorn 和 Flask 服务器,从跨语言模型保留 : https://github.com/facebookresearch/XLM

该模型需要"fastBPE"库(https://github.com/glample/fastBPE),该库需要使用以下命令进行安装: g++ -std=c++11 -pthread -O3 fastBPE/main.cc -IfastBPE -o fast

但是,由于 Heroku 服务器是为 Python 配置的,因此它无法识别"g++"命令。

这是我到目前为止尝试过的: - 在 Heroku 中添加构建包 "heroku-buildpack-apt" 并在我的源文件中创建一个 "Aptfile",在其中编写"g++",以及"build-essential" - 在主 python 文件中,我创建了一个子进程来启动"apt-get install g++":

import subprocess
process = subprocess.Popen("apt-get install g++", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(output, err) = process.communicate()
#This makes the wait possible
p_status = process.wait()
#This gives the output of the command being executed
print("Command apt-get output: ",output)

但是,每当我运行以下子进程来安装 fastBPE 包时:

import subprocess
process = subprocess.Popen("g++ -std=c++11 -pthread -O3 tools/fastBPE/fastBPE/main.cc -IfastBPE -o tools/fastBPE/fast", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(output, err) = process.communicate()
p_status = process.wait()
print("Command apt-get output: ",output)

我系统地得到"g++:未找到"作为输出。

此外,命令"哪个 g++"不返回任何内容,但"哪个 gcc"返回"/usr/bin/gcc",因此安装了 gcc 但不返回 g++

我最终设法弄清楚了。

对于后代,有 2 种解决方案对我有用:

1 - 不太好的方法是在与 Heroku 服务器环境完全相同的 Linux 计算机上执行 g++ 命令,将其推送到 Heroku 并确保之后永远不会修改它。然后,您可以使用上述子流程调用fastBPE ==>它有效,但它更像是一个DIY不稳定的解决方案。关联的 GitHub 主文件 https://github.com/Tony4469/xlm-agir/blob/master/mlm_tlm_prod.py

2 - 最好的解决方案是使用 Miniconda 环境预编译 Docker 容器上的所有内容,您可以安装和运行所有必要的命令,然后将其轻松推送到 heroku。你可以在这里找到我使用的Dockerfile: https://github.com/Tony4469/laser-agir/blob/master/Dockerfile