# Stop executing Python code

**URL:** <https://discourse.nodered.org/t/stop-executing-python-code/15431>\
**Category:** General\
**Created:** [11 September 2019 18:32 UTC](https://discourse.nodered.org/t/stop-executing-python-code/15431 "2019-09-11T18:32:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lodv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lodv/32/7963_2.png) [@lodv](https://discourse.nodered.org/u/lodv)\
**Post date:** [11 September 2019 18:32 UTC](https://discourse.nodered.org/t/stop-executing-python-code/15431/1 "2019-09-11T18:32:21Z")

</div>

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 camerabeweging.py")\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 relaismodule.py")\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.  
Yvonne

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [11 September 2019 18:49 UTC](https://discourse.nodered.org/t/stop-executing-python-code/15431/2 "2019-09-11T18:49:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [11 September 2019 20:10 UTC](https://discourse.nodered.org/t/stop-executing-python-code/15431/3 "2019-09-11T20:10:34Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![lodv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lodv/32/7963_2.png) [@lodv](https://discourse.nodered.org/u/lodv)\
**Post date:** [11 September 2019 20:50 UTC](https://discourse.nodered.org/t/stop-executing-python-code/15431/4 "2019-09-11T20:50:28Z")

</div>

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.  
Yvonne

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [11 September 2019 20:54 UTC](https://discourse.nodered.org/t/stop-executing-python-code/15431/5 "2019-09-11T20:54:12Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![lodv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lodv/32/7963_2.png) [@lodv](https://discourse.nodered.org/u/lodv)\
**Post date:** [12 September 2019 00:36 UTC](https://discourse.nodered.org/t/stop-executing-python-code/15431/6 "2019-09-12T00:36:23Z")

</div>

Hi Lena,

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