ruby-on-rails – Is it possible to have python scripts coexist in the rails web system?

Question: Question:

Currently, we are developing a web service with Ruby on Rails.
* In-house tool for BtoB

However, due to the following factors, we would like to implement some functions in python.

-When performing advanced numerical calculations on a website, python is easier than ruby, and python has the know-how.

・ The members who cooperate are experienced python and there are several inexperienced Ruby.

I thought about the option of doing everything with python, but in the end I would like to do it with rails as the main and partly with python.

Is such a method possible?

I wasn't sure if I could call a python script from a helper method like link_to in rails.
* Not limited to link_to, but I would like to do so if page transitions and dynamic python scripts can be executed.

Thank you.

Answer: Answer:

There are many ways to do it. If you write in simple order

  1. Call a Python program from Ruby using interprocess communication such as IO.popen
  2. Launch another web behind the scenes with Flask, Django, etc., and access it from RoR with a reverse proxy.
  3. Create a task queue with redis etc., and add the calculated task to the task queue together with the job number from RoR. Notify the browser of the job number. Python calculates each time a task is entered and saves the result in the DB using the job number as a key. The browser queries RoR for the result by job number. Show status
    (Waiting for calculation, calculating, calculated). If it has been calculated, the result is displayed.

Method 3 is especially useful when, for example, Python calculations are heavy and take more than a few minutes. You can instantly return a response to the browser without waiting for the calculation to finish. You can also limit the number of compute tasks running at the same time so that the compute server is not overloaded. If the load becomes high, it is possible to increase the number of calculation servers (workers) for distributed processing.

Scroll to Top