如何将独立Python脚本集成到Rails应用程序中? [英] How to integrate a standalone Python script into a Rails application?

查看:134
本文介绍了如何将独立Python脚本集成到Rails应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它有一个小文件结构,然后使用

  python do_work.py foo bar 

我希望我的Rails用户按下一个按钮并让它发生,结果是或者上传到某个地方,或者作为下载链接或类似的东西 - do_work.py 的输出(例如,它是 result.txt )



我也想澄清一下,脚本导致在文件系统上创建3个单独的文件,这些文件不是文本文件应该无关紧要,并不是真正的问题)



最好的方法是什么?可以耙运行exec Python吗?更重要的是,这是可行的heroku?



我的系统上安装了Python,但由sockmonk提供的答案似乎并不奏效 - 它返回nil。请注意,像 ls 之类的其他命令似乎可行。



这可能是权限问题吗?

  def index 
value =%x(python --version)
render:text =>值
结束

顺便提一句,在 irb

 %x(python)

显示irb的Python终端INSIDE。然而,无论什么原因,它都不会带参数。

index 方法不起作用,因为 python --version 将其版本输出到STDERR,而不是STDOUT。如果你不需要分离这些流,你可能只是将STDERR重定向到STDOUT:
$ b $ pre $ value =%x(python - -version 2>& 1)

这个调用是同步的,所以在运行脚本之后( python do_work.py foo bar 2>& 1 ),你应该可以读取它产生的文件。

如果脚本由于某种原因无法创建文件,现在您将在变量中看到异常,因为错误消息通常发送到STDERR。



如果要将STDERR与STDOUT分开,请使用 Open3 模块。



注意脚本需要一些时间才能运行,因此这些调用可能会重叠。我会在这里使用队列来阻止这种情况。



不要忘记检查用户输入的数据。切勿直接将其传递给脚本。


I've got a program that has a small file structure going on and is then ran using

python do_work.py foo bar

I want my Rails users to press a button and have this happen for them, with the result either uploaded somewhere or just thrown to them as a download link or something of the sort - the output of do_work.py (say, it's result.txt)

I also want to clarify that the script results in the creation on the filesystem of 3 separate files, which are not text files (which shouldn't matter and isn't really the problem here)

What is the best way to go about it? Can rake run exec Python? More importantly, is this doable on heroku?

I have Python installed on my system but the provided answer by sockmonk doesn't seem to work - it returns nil. Mind you, other commands like ls seem to work.

Could it be a permissions problem?

def index
    value = %x( python --version )
    render :text => value
end

Incidentally, trying this in irb:

%x(python)

Brings up the Python terminal INSIDE of irb. It will not take params for whatever reason however.

解决方案

Your index method does not work because python --version outputs its version to STDERR, not STDOUT. If you don't need to separate these streams, you may just redirect STDERR to STDOUT:

value = %x(python --version 2>&1)

This call is synchronous, so after running the script (python do_work.py foo bar 2>&1), you should be able to read the files produced by it.

If the script is not able to create the files for some reason, you will now see the exception in the value variable because error messages are usually sent to STDERR.

If you want to separate STDERR from STDOUT, use the Open3 module.

Beware that the script takes some time to run, so the calls may overlap. I would use a queue here to prevent this.

And don't forget to check the data the user enters. Never pass it directly to the script.

这篇关于如何将独立Python脚本集成到Rails应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆