Stop executing Python code

I use a switch node, which is attached to a switch node (different kind) in order to execute pythoncode 1 or 2, depending if the first switch is on or of. The first code runs in a while True loop as long as the switch is not off. But when I switch it off in order to run different code, the first code is also running. How can I stop the first python script, so that only the second script is running?
This is the code:
[{"id":"2f9b4dad.ce2a72","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"9f30a79b.abaee8","type":"python-function","z":"2f9b4dad.ce2a72","name":"relaiscamera","func":"os.system("sudo python")\nreturn msg\n\n","outputs":1,"x":530,"y":200,"wires":[]},{"id":"c9a4faf2.54ec68","type":"switch","z":"2f9b4dad.ce2a72","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":240,"wires":[["9f30a79b.abaee8"],["aec54810.d69248"]]},{"id":"6f234a73.ff5574","type":"ui_switch","z":"2f9b4dad.ce2a72","name":"","label":"Relais switch","tooltip":"","group":"30cf61f4.af3dde","order":10,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":170,"y":240,"wires":[["c9a4faf2.54ec68"]]},{"id":"aec54810.d69248","type":"python-function","z":"2f9b4dad.ce2a72","name":"relaisalleen","func":"os.system("sudo python")\nreturn msg\n\n\n\n","outputs":1,"x":530,"y":260,"wires":[]},{"id":"30cf61f4.af3dde","type":"ui_group","z":"","name":"Main","tab":"b5e43b1e.fba5c","order":1,"disp":true,"width":"6","collapse":false},{"id":"b5e43b1e.fba5c","type":"ui_tab","z":"","name":"Main","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
Thank you in advance.

Linux or Windows? You can kill the first python process and start a new one, but this can get expensive if the switch happens a lot.

I use MQTT to communicate between node-red and a Python daemon process.

The idea is simple:
Node-red publishes topic, say, Switch_State with message of A or B. The Python code subscribes to topic Switch_State and the while loop can combine both programs with an If-Else on SwitchState value.

Its readily expandable as things evolve and you and add a message to tell the Python to exit, etc.


Hi Yvonne,
Is there a specific reason why you use an os.system call to execute another python script, when you could do the same with an exec node instead? I haven’t really played with python-function yet, but have you thought about using the subprocess library instead, or perhaps import the script then execute the __main__ in it that way.

I mostly write MQTT wrappers around my python code, wrote one last week to control systemd services on a pi, where a command structure is used to interact with specific services. These are then run through subprocess

I’m working on a Raspberry Pi. Maybe MQTT is the way to go. I will try it tomorrow. For me this stuff is all so new and I have tried to figure out how I can solve this for days now. I used Python in some applications, but Node-red is still rather new for me and I have to find out how all the nodes work.
Thank you very much for the help.

Hi Yvonne, I can post a sample MQTT wrapper I wrote last week tomorrow with a flow to show you how it works. If you prefer typing/answers in Dutch that’s possible too :slight_smile:

Hi Lena,

That would be great. Yes, please.
Thank you.
Heel veel dank.