Running a Python Script in Rails

My question might be rather simple:

  • Where should I put python script files that I made, to run on a rails on that rails server?

  • And how do I call the script? ( I am looking for what is the path directory )

  • I have tried to run it on my local machine in which I can store the files anywhere but I really want to follow some type of good convention for doing this on an enterprise server. :/

    Right now I have it sitting in the script folder off the root of the arch.

    The script prints to the std out since that is what Ruby sees when it runs. So the print out is just going to be stored into a variable and then I will use some functions to figure out the csv string fields and then sorts it into it's corresponding db bins.

    But I really am having trouble figuring out where to put the files. There are like no hits on my searches for this specifically. Either it's really easy and I missing something or just no one cares to do this...


    Edit: Thanks for the replies but what I said must not have been totally clear so I edit the bullets.

  • Where do I put the python script (program) ON the rails ( folder-wise )? Is there a folder that I should put it that is considered convention. I had it in app/assets/python before and I got error from rails trying to use it as if it was a normal styling file.

  • If I stored it as: script/python/do-it.py and I wanted to call it what is the path to the location of that file? Is it just as I wrote it or something else because I can't get it to run...

  • And just to mention.. Using backticks works the best for me I am thinking, I already got that work on local ruby test file, BUT I'm trying to do this on rails with the best conventions that are supposed to be used.... The thing is... I don't know what the conventions are...

    So simply this is kinda what I have done (slightly changed for privacy)

    <#% result = Array.new %>
    <#% Thread.new do %>
      <#%= result = `python /home/me/RailsApp/script/py_file.py #{param} &` %>
    <#% end %>
    <p><#%= puts result %></p>
    

    or is it?

    <#% result = Array.new %>
    <#% Thread.new do %>
      <#%= result = `python script/fixitfelix/py_file.py #{param} &` %>
    <#% end %>
    <p><#%= puts result %></p>
    

    I'm going to double check to see if I answered my own question.

    I had to use a thread thing. The script runs for a bit and it would hang the whole server while it is running.


    Thanks J Plato, and I just want to spell this out for anyone searching this exact question.

    To run some py_test.py script, I put the script in:

    lib/assets/py_test/py_test.py
    

    and ran it by:

    result = `python lib/assets/py_test/py_test.py`
    

    Note: I used the backtick kernel way.

    Since the python file that I was running takes time to work, in which if I just used the straight backtick code, it hangs the server until the script finishes. The way I got around this was to make a thread. I'm not sure if this is the most efficient way of doing it, but it works so far for my needs. The thread code is above, in the original question. In the code block, it is in commented out html.erb script since that was how I originally tested it and didn't modify it, sorry if that's confusing...

    Thanks everyone post more if you have anything else to say or correct me. I will keep an eye on this post for that :)

    链接地址: http://www.djcxy.com/p/25312.html

    上一篇: 如何从GET参数中获取值?

    下一篇: 在Rails中运行Python脚本