[pre-alpha] Running python within Node-RED simply (or even Rust)

Hello all,

I took the time to finally develop a set of nodes to execute Python in a way that should be "simple" for automation techs, but also their IT teams who set up Node-RED or FlowFuse.

==> GitHub - hyamanieu/node-red-python-tasks: A simple way to include python into your pipelines and Node-RED flows, even when executing in the cloud or using external workers. · GitHub

The idea is that by simply installing this package, it automatically adds a Python workspace manager called pixi on the Node-RED host machine. Then Python environments (or workspaces in pixi lingo) are installed or updated on the fly from within Node-RED. It should not be necessary to execute any command from the host terminal to set it up: it should be installable from the palette manager.

In later phases of the roadmap, executing code on remote machines is planned. I believe it should be as simple. The philosophy is simple:

  • an automation tech should not have to learn DevOps skills
  • they should be able to execute code in most languages without slowing down production
  • their scripts should be able to receive and send payloads to a running Node-RED instance

I have asked around me and I know some automation programmers struggle with the exec node, notably because they need to add Python to their host machine. Then they only execute batch tasks once in a while. Running many tasks within seconds is still a problem. It shouldn't be anymore with this package.

By automation programmers I really mean people who focus on PLC programming and sprinkle their work with some Python/.NET scripts, not Python DevOps. With the diversity of machine learning libraries, documentation, and LLMs, it has become realistic for most of them to add online data inference within their Node-RED flows.

I have two questions for the community:

  • Is it useful, and should it be published to npm?
  • If yes, would there be an interest in adding Rust on the roadmap? As it uses pixi in the background, executing Rust code is possible, and it seems this programming language is also gaining momentum within the PLC programming world.

I am open to any critique/question/feedback. Or contrib :grin:

Note: the repo was mostly "vibe coded", but everything has been thoroughly and humanly tested and corrected (by me).

I'm not so sure. Full respect what you have made but for me, running quite a few Python scripts "mqtt-integrated-with-NodeRED" but running fully outside in own processes, the exec-node has satisfied all me needs so far

Does your nodes support Python multithreading as well as multiprocessing?

Kind regards, Walter

Thanks for responding!

Does your nodes support Python multithreading as well as multiprocessing?

It's on the roadmap, but it actually already does it when sending to different workspaces.

mqtt-integrated-with-NodeRED

What do you mean here? You have a link?

Without wanting to lessen what you've achieved. Just a note of caution from experience in an enterprise setting.

The more levels of execution you have, the harder it is to secure things.

What protections do you put in place to make sure that people can't accidentally or deliberately cause mayhem by executing "random" external code? Or indeed, not so random, especially on a compromised machine or account.

Sadly, this is no longer a theoretical or remote possibility - just ask the organisations across the EU hit by freezer hacks recently.

So anyway, just a friendly warning to anyone going down this route - the implementation might be easy - possibly too easy in fact. The secure architecture is probably not so easy.

As the creator of Erlang-Red, I've gone one step further and decided to implement the entire backend in Erlang instead of Javascript. That means the function node over at Erlang-Red is actually Erlang code, no Javascript allowed!

Why do this? Because Erlang is far better suited to the concepts of flow based programming but the Node-RED editor is unbeatable! Erlang also has additional concepts (such as Supervisors and State Machines) that aren't available in JS but that are well suited to a visual approach to programming, so I've created nodes to represent those concepts.

I think there is more to gain in porting the visual approach/experience of Node-RED to Python than to add a python node to Node-RED. As pointed out, I can use an exec node for that or even create a wrapper around it to make it simpler (I did also create a ExecWithPipe so that I could pipe msg.payload to a python process to execute the python code I create in Node-RED - for example).

Also since I want to share flow code across Erlang-Red and Node-RED, i.e. the same flows should ideally be executable in both, I've also created a simpler code sharing setup - FlowHub Local.

But these are all things that I like doing, AI got bored of my ideas and won't help me.

If it's useful for you, then publish it to npm. If not, then leave it. If you're not going to maintain it, then leave it.

If you ask, you'll get (NumberOfPeopleWhoRespond + 1) opinions - just publish it!

There is a Rust-RED i.e. a complete implementation of the Node-RED concepts in Rust.

P.S.

oooooo yes:

That's also why I ported the concept to Erlang - it's amazing multi-processing with thousands/millions of processes. This means that each node becomes it's own process and each message (msg) is a true message passed between independent processes. I.e. Instead of using "events", I'm using true messages being passed between processes .... yet the flows look the same!

I just ment that all two-way communication between Node-RED and all my Python scripts is with MQTT

This was in the back of my mind, but I thought it can be delayed:

  1. I wanted to focus on having a functional MVP first
  2. The issue is the same with exec or even function node (albeit not for executing code on remote machines, but that would involve the IT team)
  3. Industrial IT teams must be already aware of security risks and contain them at another level

One idea was to use artifact keeper. With it, responsibility to let images or packages go through is delegated to another specialised team. IT and OT team arguing is at the core of cybersecurity, right :grimacing:?

I'm impressed by the rust and Erlang versions of Node-RED. I must say, the first time I found out about Node-RED I thought "but why JavaScript?" (Like many, I know)

Anyways, the take away message is that multiprocessing/threading should be my next target.

I haven't checked how hard it is to install and use a no-GIL Python version, but with it multithreading would be enough.

I would need to change my code anyways.

Thanks!

Good. :wink:

Having played around with Node-RED for the last few years, that is really my take away: it represents a way of programming in parallel (concurrently) that is simple and understandable. Its representation makes complex message passing architecture look like Dali paintings - far more understandable than reams and reams of textual code that is dotted with message sending and receive boiler-plate code.

Coming from a Rails background, having a task queue that was as easy as calling queue_up (or something) on an object was a revelation - how simple! All it took was a couple of database tables and an extra background task that dequeued jobs ... and then handled those jobs ... how simple! And what a "simple" and "clean" architecture, rails app, database and task processor (that is scalable) ... all those moving wheels that just worked!

To do the same in Node-RED, it's a matter of adding a node and connecting it to the right spot in the flow. Done. A parallel task is created, completely visually and completely independent.

That, IMHO, is the real benefit of Node-RED - the simplicity of the UI to create concurrent jobs/tasks/processes/events and not only design data-flows (just as an added bonus!).

I'm afraid that, again from experience, I would say that isn't true at all. You've only got to look at the recent massive security failures to realise this.

I'm not suggesting you need to fix these things yourself, but you should point out the issues. That way, at least nobody can later point the finger at you if there is a security failure.

Lets not forget that, unlike most home automation (with some exceptions), industrial IoT failures can easily kill people or at least cost a LOT of money. The stakes are high.

Seems sensible to use a tool like that. And do use external reverse proxies with IdM tooling as well, don't just try to rely on Node-RED's built-in security.

As for IT - well, lets just say that I worked in just about every type of IT, architecture, security and knowledge related area of IT over my 40+ years and I was always disappointed with the average attention to detail which is often the primary cause of security failures. :person_shrugging:

Because it is the language of the web and node.js makes it easy to manage and run on servers or desktops, even Android and microprocessors at a pinch.

How many people know erlang?

Arguably, Python would have been a better foundation purely down to the number of people who learn it. But it is hard to beat the speed to delivery of a web microservice written in node.js

And once again another example of Human Lemmings: since it's the language that the most use, it must be the language we use.

The IT industry is full of bad choices because "it's the language that everyone uses" and no one really takes the time to understand what the problem space actually requires. Arguably that is why we now have so much AI because those that made those decisions ended up with artificial complex solutions because the original language wasn't the right choice for the problem and no one wanted to put in the effort to fix that choice. So now AI is aimed at these complex solutions and tries to provide answers to the ultimate question of the IT Industry today: what does this code actually do?

And how many people actually use visual coding as offered by Node-RED? Far fewer than Erlang since Erlang is the language that actually provides the infrastructure that runs our telecommunication (and probably much of the financial world and strangely gambling).

.Node-RED isn't exactly the "language" that has made great in-road since the AI "revolution" which is largely based on textual coding.

Careful, using mirco processes that Erlang offers is far far faster than single-threaded microservices with a constant overhead of network communication but hey, if everyone is doing it, it must be right.

Sorry, off-topic now.

Then please don't pose questions such as

Because you become liable to receiving answers.