简体   繁体   English

从 Flask Web 应用程序中运行 perl 脚本

[英]Run a perl script from within Flask Web Application

I have a Flask Web Application that is periodically receiving JSON information from another application via HTTP POST.我有一个 Flask Web 应用程序,它通过 HTTP POST 定期从另一个应用程序接收 JSON 信息。

My Flask Web Application is running on a CentOS 7 Server with Python 2.7.X.我的 Flask Web 应用程序在装有 Python 2.7.X 的 CentOS 7 服务器上运行。

I am able to parse the fields from this received JSON in the Flask Web Application and get some of the information that interests me.我能够从 Flask Web 应用程序中接收到的 JSON 中解析字段,并获得一些我感兴趣的信息。 For example: I get some JSON input and extract an "ID":"7" field from it.例如:我得到一些 JSON 输入并从中提取一个 "ID":"7" 字段。

What I want to do now is run a perl script from within this Flask Web Application by using this "ID":"7".我现在想要做的是使用这个“ID”:“7”从这个 Flask Web 应用程序中运行一个 perl 脚本。

Running 'perl my_perl_script.pl 7' manually on the command line works fine.在命令行上手动运行'perl my_perl_script.pl 7'工作正常。 What I want is for the Flask Web Application to perform this automatically whenever it receives an HTTP POST, by using the specific ID number found in this POST.我想要的是 Flask Web 应用程序在收到 HTTP POST 时自动执行此操作,方法是使用此 POST 中找到的特定 ID 号。

How can I do that in Flask?我怎么能在 Flask 中做到这一点? Is it a good idea to do it with a subprocess call or should I consider implementing queues with Celery/rq?子进程调用来做它是个好主意还是我应该考虑用 Celery/rq 实现队列? Or maybe some other solution?或者也许是其他一些解决方案?

I think the perl script should be invoked as a separate Linux process, independent of the Flask Web Application.我认为 perl 脚本应该作为一个独立的 Linux 进程被调用,独立于 Flask Web 应用程序。

Thank you in advance :)先感谢您 :)

Sub,子,

I vote yes on subprocess, here's a post on SO about it.我对子流程投赞成票,这里有一篇关于它的帖子 Control remains with Flask that way.控制权仍然在 Flask 那里。 An alternative might be to code a perl script that watchdogs for a trigger event depending on your needs, but that would put more of the process control on the perl side of things and less efficient use of resources.另一种方法可能是编写一个 perl 脚本,根据您的需要监视触发事件,但这会将更多的过程控制放在 perl 方面,并且资源的使用效率较低。

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

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