如何从服务器端Javascript执行Python脚本 [英] How to execute a Python script from server side Javascript

查看:379
本文介绍了如何从服务器端Javascript执行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何从客户端执行Python脚本,有许多答案。我有兴趣了解是否可以从服务器端执行脚本并检查执行是否已成功完成。假设我正在使用 Meteor 堆栈,它在两端都使用JavaScript,并且有一堆Python脚本任务需要从后端触发。

There are a number of answers in relation to how one can execute a Python script from the client side. I am interested in finding out if it is possible to execute the script from the server side and check if the execution has finished successfully. Let say that I'm using Meteor stack which uses JavaScript on both sides and there are a bunch of Python script tasks that needs to be triggered from backend.

推荐答案

如果你需要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

这篇关于如何从服务器端Javascript执行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆