gpt4 book ai didi

javascript - Firebase Cloud Functions 运行 Python 脚本

转载 作者:行者123 更新时间:2023-12-03 23:06:35 24 4
gpt4 key购买 nike

我有一个 Express 应用程序,它生成一个 Python 进程来执行 Python 脚本。当我做 firebase serve ,我可以看到我的端点被击中,然后运行 ​​Python 进程。但是,该过程似乎没有执行。

const runPythonScript = () => {
return new Promise((resolve, reject) => {
let value;
const spawn = require('child_process').spawn;
const pythonProcess = spawn('python', ['./myScript.py']);

pythonProcess.stdout.on('data', (data: string) => {
console.log('Am I being hit?') // This line is not being hit
value = JSON.parse(data);
});

pythonProcess.on('exit', (code: number) => {
if (code === 0) {
resolve(value);
}
else {
reject(value);
}
});
});
}

从上面代码中的注释来看,stdout 'data' 的监听器没有被命中。我对 Firebase 不太熟悉,但我的想法是使用 Firebase Hosting 作为我的前端,然后使用 Firebase Cloud Functions 来运行我的 Express 服务器。为了让我的应用程序运行 Python 脚本,我需要做些什么吗?

根据我从其他 StackOverflow 帖子 ( here) 中收集到的信息,我无法运行 Python 进程,可能是因为 Firebase Cloud Functions 没有安装 Python。因此,我需要将我的 Python 脚本打包成一个可执行文件(如 here 所述),以便 Firebase Cloud Functions 可以运行该可执行文件。这个对吗?如果是这样,我宁愿不必打包我所有的 Python 脚本。有没有更好的方法来处理这个问题?它是免费的吗?

最佳答案

From what I've gathered from other StackOverflow posts, I can't run a Python process, perhaps because Firebase Cloud Functions does not have Python installed.



这是真实的。

So instead, I need to package my Python script into an executable (as described here), so that Firebase Cloud Functions can just run the executable. Is this correct?



你当然可以试试这个,但我不推荐它。听起来很多工作都没有什么好处,尤其是当您有其他选择时。

Is there a better approach to handling this?



您可以在 python 中本地编写 Cloud Functions。您将无法使用 Firebase 工具来测试和部署它们。 Google Cloud has everything you need to get started .

关于javascript - Firebase Cloud Functions 运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62327293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com