且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从服务器端Javascript执行Python脚本

更新时间:2023-11-27 22:17:40

如果你需要python脚本项目,最常见的方法是连接python和流星通过消息队列。例如,应该触发一些python脚本的流星发生的动作。您将消息发送到队列以进行python。 Python监听队列以及何时获取您的消息启动任务。任务完成后,python应该向队列发送消息,可能带有任务结果或者其他。

If you need python scripts at you project the most common way is to connect python and meteor through message queue. For example on meteor occured action which should trigger some python script. You send message to queue for python. Python listening queue and when get your message starts task. After task is done, python should send message to queue, maybe with results of task or else.

//Meteor server side
var amqp = Meteor.require('amqp');
var connection = amqp.createConnection(amqpCredentials);
var Fiber = Npm.require("fibers");

connection.on('ready', function(){
    connection.queue(queueName, {autoDelete: false}, function(queue){

      console.log(' [*] Waiting for messages. To exit press CTRL+C')

      queue.subscribe(function(msg){
          console.log(" [x] Received %s", msg.data.toString('utf-8'));
          var msg = EJSON.parse(msg.data);
          if(msg.type === 'news'){
            Fiber(function(){News.insert(msg.data).run()});
          }
      });
  });

});

在python方面你应该运行任务和添加队列侦听器。
您可以在官方文档 RabbitMQ导师>中阅读有关RabbitMq和python客户端的信息a>

At the python's side you should run tasks and add listener of queue. You can read about RabbitMq and python client at official documentation RabbitMQ tutor