Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to get python and meteor talking [closed]

Tags:

python

meteor

I would like to build a project in meteor (version 0.8) that calls a python script which in turn sends some data back to meteor. I am not sure what the best practice is for doing this at the moment.

DDP looks good:
"Clients that can be used to communicate with Meteor through it's DDP protocol, from outside the Meteor stack." But the python implementation looks unfinished: python-ddp-client

I guess I could also write directly to mongodb from python but it doesn't sound like the best idea:

  • Can Python write to database and Meteor reactively update
  • How do I access Meteor's mongodb from another client, while meteor is running?

Am I missing anything? Is there a better way to do this?

like image 497
e h Avatar asked Apr 11 '14 11:04

e h


2 Answers

If the python script is on the same server you could just call it like in a normal Node.js application:

var exec = Npm.require('child_process').exec;
var Fiber = Npm.require('fibers');
var Future = Npm.require('fibers/future');

Meteor.methods({

  callPython: function() {
    var fut = new Future();
    exec('pythonScriptCommand with parameters', function (error, stdout, stderr) {

      // if you want to write to Mongo in this callback
      // you need to get yourself a Fiber
      new Fiber(function() {
        ...
        fut.return('Python was here');
      }).run();

    });
    return fut.wait();
  },

});
like image 73
Hubert OG Avatar answered Nov 14 '22 20:11

Hubert OG


The other questions you linked to are very out of date (one is almost two years old).

You should just write directly to MongoDB from python, if you don't need to call server methods. In fact, this is how multiple Meteor servers (load-balancing the same app) talk to each other. This is implemented by having Meteor servers tail the Mongo oplog and incorporate any database operations immediately.

This was fully implemented in Meteor version 0.7.2.

When you write to the database, you can use the typical observe or observeChanges operations on collections to have the Meteor server do stuff.

Another way to make RPC calls is to use a messaging bus such as ZeroMQ. I'm using this to call machine learning algorithms in Python from Meteor. This supports Python processes on different machines, load balancing, and the like. See the following post for how to do so:

http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

like image 32
Andrew Mao Avatar answered Nov 14 '22 20:11

Andrew Mao