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

在Meteor项目中,如果需要运行Python脚本,通常可以通过消息队列实现服务器端的异步执行。当Meteor后端触发特定事件时,发送消息到队列,Python监听该队列并开始执行任务。任务完成后,Python将结果回传到队列。此示例中提到了使用RabbitMQ作为消息队列进行Python和Meteor之间的通信。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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.

解决方案

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

}

});

});

});

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值