简体   繁体   English

使用heroku bash连续运行python脚本

[英]Running python script with heroku bash continuously

I have a script that requires 2 two inputs that are not the same every time.我有一个脚本需要 2 个每次都不相同的两个输入。 So I have to give the input manually every time I run the script.所以我每次运行脚本时都必须手动输入。 And I do want to deploy this script to Heroku.我确实想将此脚本部署到 Heroku。 But unfortunately, I can't give any kind of input manually in heroku:worker.但不幸的是,我无法在 heroku:wor​​ker 中手动提供任何类型的输入。 I can't use any kind of environment variables, arguments because these 2 inputs are not static.我不能使用任何类型的环境变量和参数,因为这两个输入不是静态的。 So heroku:worker just keeps getting the 'There are no inputs' error.所以 heroku:wor​​ker 只是不断收到“没有输入”错误。

So I have decided to run the script with the command heroku run python3 main.py but no matter what I do I couldn't get out of the terminal without ending the process.所以我决定使用命令heroku run python3 main.py运行脚本,但无论我做什么,我都无法在不结束进程的情况下离开终端。 I want this script to run continuously even if I get out of the terminal.我希望即使我离开终端,这个脚本也能连续运行。

So how can I run the python script in Heroku continuously and quit the terminal without breaking the process?那么如何在 Heroku 中连续运行 python 脚本并退出终端而不破坏进程呢?

As Alexander stated Heroku script deploys are not meant to be run frequently like this.正如亚历山大所说,Heroku 脚本部署不应该像这样频繁运行。 It will be worth your time to create a simple Flask server to accept HTTP requests from anywhere (so you can then run your script via CURL or Postman).值得花时间创建一个简单的 Flask 服务器来接受来自任何地方的 HTTP 请求(这样您就可以通过CURL或 Postman 运行您的脚本)。 Heroku also has very good tutorials on how to do this if you are unfamiliar.如果您不熟悉,Heroku 也有关于如何执行此操作的非常好的教程。

However if this is a hard requirement that you cannot deploy a simple web server (even though, you really should), let's work around this:但是,如果这是您无法部署简单 Web 服务器的硬性要求(尽管您确实应该这样做),那么让我们解决这个问题:

if you are on OSx or any Unix style machine you can background a process with a simple process script & ie, heroku run my_script.py & and to call it to the foreground again, just type fg .如果您使用的是 OSx 或任何 Unix 风格的机器,您可以使用一个简单的process script &作为后台进程process script &即, heroku run my_script.py &并再次将其调用到前台,只需键入fg

I am less familiar with windows but i think you can do start /b <program> but in your case you will be better served by the answer provided here How to run a command in the background on Windows?我对 Windows 不太熟悉,但我认为你可以做start /b <program>但在你的情况下,你会更好地得到这里提供的答案如何在 Windows 的后台运行命令? (if you are running on windows) (如果您在 Windows 上运行)

Note this is a bad solution and your script will stop running once you close your user session/shut down.请注意,这是一个糟糕的解决方案,一旦您关闭用户会话/关闭,您的脚本将停止运行。

The proper way to be create a web server which takes requests on demand - that way you can pass whatever params you need to whenever you need the script to run.创建按需接收请求的 Web 服务器的正确方法 - 这样您就可以在需要运行脚本时传递所需的任何参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM