<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Running Python under node.js]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">A few weeks ago I started looking into getting notifications onto the mirror from python.  Whilst it was relatively straight forward to run python within a shell from the node-helper, I ran into various problems with user privileges and environments.</p>
<p dir="auto">To get around this I had a play with rabbitmq.  Using amqp for the node end and pika for the python.  This opens up a whole world of possibilities for my mirror without having to learn javascript (above the basics &amp; plagiarising code snippets from the built in modules) .</p>
<p dir="auto">node-helper end looks like.  Sorry it’s a bit messy &amp; I went overboard on the error capture.</p>
<pre><code>var amqp = require("amqplib/callback_api");

PStart: function starter() {
const self = this;
  amqp.connect("amqp://localhost", function(err, conn) {
    if (err) {
      console.error("[AMQP]", err.message);
      return setTimeout(starter, 1000);
    }
    conn.on("error", function(err) {
      if (err.message !== "Connection closing") {
        console.error("[AMQP] conn error", err.message);
      }
    });
    conn.on("close", function() {
      console.error("[AMQP] reconnecting");
      return setTimeout(starter, 1000);
    });
    console.log("[AMQP] connected");
    amqpConn = conn;

  amqpConn.createChannel(function(err, ch) {
    if (closeOnErr(err)) return;
    ch.on("error", function(err) {
      console.error("[AMQP] channel error", err.message);
    });
    ch.on("close", function() {
      console.log("[AMQP] channel closed");
    });

    ch.prefetch(10);
    ch.assertQueue("message", { durable: false }, function(err, _ok) {
      if (closeOnErr(err)) return;
      ch.consume("message", function(msg) {
try {
 var fp = JSON.parse(msg.content.toString());
    ch.ack(msg);
    } catch (e) {
      closeOnErr(e);
    }
                if (fp[0].data == "data"){
                self.sendData({"PlrStat": fp[0].PlrStat, "TrkNxt":fp[0].TrkNext, "Station": fp[0].Station, "Said": fp[0].Said);
                } else if (fp[0] == "status") {
                console.log("[MMM-Radio]" + fp[0].status);
                } else {
                console.log("Got something");
                console.log(msg);
                console.log(fp[0].data);}
})}, { noAck: false });
      console.log("Worked");
      });
});
}  });

function closeOnErr(err) {
  if (!err) return false;
  console.error("[AMQP] error", err);
  amqpConn.close();
  this.Pstart();
  return true;
}

</code></pre>
<p dir="auto">Python end:</p>
<pre><code>import pika

def to_node(message):
    try:
        channel.basic_publish(exchange='',
                              routing_key='message',
                              body=json.dumps(message))
    except Exception:
        connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
        channel = connection.channel()
        channel.queue_declare(queue='message')
        channel.basic_publish(exchange='',
                              routing_key='message',
                              body=json.dumps(message))

</code></pre>
<p dir="auto">The bit in the exception is the initial setup for the connection, it does occasionally time out so re-establish if the publishing doesn’t work.</p>
<p dir="auto">Some useful tutorials on rabbitmq can be found <a href="https://www.rabbitmq.com/getstarted.html" target="_blank" rel="noopener noreferrer nofollow ugc">here</a></p>
<p dir="auto">Hope this is a help to some of you who know very little about js &amp; do all the complicated stuff with other languages.</p>
]]></description><link>https://forum.magicmirror.builders/topic/9552/running-python-under-node-js</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 09:29:27 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/9552.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 20 Jan 2019 13:57:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Running Python under node.js on Wed, 23 Jan 2019 10:18:44 GMT]]></title><description><![CDATA[<p dir="auto">Node js is one of the powerful tools of programming and using this if you are working on python it help you a lot. <a href="https://epsonsupports.net/blog/how-to-fix-printer-offline/" target="_blank" rel="noopener noreferrer nofollow ugc">printer offline fix</a> helped me to know more about python.</p>
]]></description><link>https://forum.magicmirror.builders/post/50647</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/50647</guid><dc:creator><![CDATA[timpaines]]></dc:creator><pubDate>Wed, 23 Jan 2019 10:18:44 GMT</pubDate></item><item><title><![CDATA[Reply to Running Python under node.js on Sun, 20 Jan 2019 15:21:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/grantc66" aria-label="Profile: grantc66">@<bdi>grantc66</bdi></a> cool, thanks…</p>
<p dir="auto">for node, you could have spun off a long running python app,<br />
and added data.on() event handlers in javascript node_helper to get called each time the python app wrote to the console (stdout)… which could have been json data, or whatever text you liked</p>
<pre><code>const {spawn} = require('child_process')

const child = spawn('pwd');

child.stdout.on('data', (data) =&gt; {
  console.log(`child stdout:\n${data}`);
});

child.stderr.on('data', (data) =&gt; {
  console.error(`child stderr:\n${data}`);
});
</code></pre>
]]></description><link>https://forum.magicmirror.builders/post/50449</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/50449</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Sun, 20 Jan 2019 15:21:35 GMT</pubDate></item></channel></rss>