1

Is there a conventional way to run python scripts from a Ruby on Rails webserver?

I have a Ruby on Rails site that is currently deployed to Heroku (I can deploy to AWS or wherever this is possible).

I would like for people to upload, and execute arbitrary python scripts on certain page requests (that we review for security). The python scripts should be relatively short running (few seconds tops), mostly to parse or process data. Ideally we would let users upload a script in any language, but python is the most important for now.

1 Answer 1

1

There are several ways to do this. The most obvious one would be to write another web service, this one in Python, but that's hardly elegant. Another solution:

Install rubypython gem.

Then you can do things like:

require 'rubypython'
RubyPython.start
my_python_module = RubyPython.import('my.python.module')
value = my_python_module.my_python_function().rubify
RubyPython.stop

You can see more at rubypython documentation pages.

A third option would be to simply shell out and execute Python as any executable, through IO.popen.

Sign up to request clarification or add additional context in comments.

3 Comments

Amazing. Thanks @Amadan! Do you know how performant rubypython is? (reading through the docs now)
What do you mean by "shell out"? I'm assuming you mean execute IO.popen through the ruby shell, although seems like that could get messier than rubypython which I could execute in a controller
"Shell out", as in, execute something in a shell (e.g. bash), like Kernel.system and IO.popen do. RubyPython bridge is definitely cleaner though, I believe. I do not know about the performance; I think RubyPython.start should be slow, and you could probably do that only once in the app initialisation stage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.