Best way to get python and meteor talking [closed]

时光怂恿深爱的人放手 提交于 2019-12-18 10:30:07

问题


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?


回答1:


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();
  },

});



回答2:


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/



来源:https://stackoverflow.com/questions/23011443/best-way-to-get-python-and-meteor-talking

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!