Call Python scripts in Meteor

What is the best way to call the meteor a python script application that is on the same machine as the server code on the meteor side? All I want to do is have a meteor passing the string to the function in python, and let python return the string to the meteor.

I thought I could have a ponton monitor mongodb and retrieve the values ​​and write them back to mongodb after calculation, but it seems cleaner to have meteor call the function directly in python.

I am new to DDP and could not get very far from python-meteor ( https://github.com/hharnisc/python-meteor ).

Is ZeroRPC ( http://zerorpc.dotcloud.com/ ) a good way to do this?

Thanks.

+5
source share
2 answers

Great question.

I reviewed the use of DDP and ZeroRPC and even wrote Python directly in Mongo.

For me, the easiest way to talk with Meteor and Python was to configure the python script as a flash application, then add the API to the flash application and talk to Meteor with Python through the API.

To use this setting, I used:

To test it, you can create something like this (python script converts text to uppercase):

 from flask import Flask from flask.ext import restful app = Flask(__name__) api = restful.Api(app) class ParseText(restful.Resource): def get(self, text): output = text.upper() return output api.add_resource(ParseText, '/<string:text>') if __name__ == '__main__': app.run(debug=True) # debug=True is for testing to see if calls are working. 

Then in Meteor use HTTP.get to test the API call.

If you use everything locally, a Meteor call will probably look something like this: Meteor.http.get("http://127.0.0.1:5000/test"); : Meteor.http.get("http://127.0.0.1:5000/test");

+6
source

I have experience in the past in implementing a similar approach using the RestFul approach.

By running observChanges from Meteor, sending an HTTP request to the api endpoints to rest Python (in Flask ) from the server, then Flask processes the requests when invoking the corresponding Python scripts / functions with the Meteor response, then processes the callback accordingly.

There are, of course, many other approaches that you can consider, for example, using DDP, child_process, etc. I also looked at using python-meteor before, after you took into account that the RestFul approach is more portable and scalable (both on the same computer and on different machines ... you can expand your servers to handle more requests and etc., you get the idea).

Each use case is different, and I found that RestFul is best suited for my use case. I hope you find my answer helpful and expand your selection of consideration and choose the one that best suits your case. Good luck.

+2
source

Source: https://habr.com/ru/post/1210328/


All Articles