Exec node send an output payload on more than one message

Hi,following this suggestion I'm using the template node to create aand save a python script, than the exec node to run it
https://flows.nodered.org/flow/0a6e073bcd6bda364fafd32699837cc7
this is my exec node


My python script has just one print command (in a while loop). Now the problem is that the message payload (which is the print command in python) is sent on multiple consecutive messages on the output 1 of the exec node. How can I deal with it and have just one output messages for each print command?
Thanks

It will be hard to help you with just the information you have provided. There could be a bug in your python code or an issue with your flow. Please export your flow and attach it to a reply.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Your combination of spawn mode and the -u flag should give you one message per print statement.

What operating system are you using?
What version of python?
What version of Node-red?

Here the flow example

[{"id":"b52450baec0df3d5","type":"inject","z":"0700cf57765c88b3","name":"","props":[{"p":"filename","v":"/home/pi/testing.py","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":155.1999969482422,"y":2338.39990234375,"wires":[["5bf43326848a329d"]]},{"id":"5bf43326848a329d","type":"template","z":"0700cf57765c88b3","name":"Progress","field":"payload","fieldType":"msg","format":"python","syntax":"plain","template":"#!/usr/bin/env python2.7\nimport time\nwhile True:\n    a=[402.17, 10.16, 17.2, 3482.43, 138.55, 27.76, 27.64, 0.29, 27.76, 3.52, 3.51, 3.68, 2.7, 4.15, 0.0, 8.98, 349.0, 592, 73, 189.77, 642.05, 8895.82, 7.19, 0, 0, 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 0.0, 0.0, 0, 2.83, 0, 0, 402.17, 10.16, 17.2, 402.17, 10.16, 0.34, 'null', 0.0, 0.0, 0.34, 0.0, 0.0, 0.0, 85.8, -4.2, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 67, 0, 0.0, 3.48, 0, 3.48, 56.8, 'null', 0.0, 0, 0, 41, 0, 0.0, 0.0, 0.0, 0.0, 5.75, 0.0, 85.51, 0.0, 0.0, 41, 0, 793, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 711, 3, -5, 3.48, 'null', 'null', 'null', 'null', 27.59, 27.59, 0.0, 0.0, 0, 0, 27.77, 27.75, 27.62, 0.0, 16.3, 16.3, 16.8, 16.8, 14.9, 7.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 555.37, 1406.65, 26.61, 26.61, 0.0, 'null', 'null', 'null', 'null', 0.0, 555.37, 0.0, 0.0, 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 0.0, 0.0, 7.9, 0.0, 0.0, 0.0, 8.66, 3482.43, 26.61, 325.98, 10.18, 27.56, 27.53, 27.76, 27.64, 396.96, 0.0, 0.0, 454.56, 'null', 0, 0, 0, 0, 0, 0, 'null', 0, 0, 0, 0, 0, 0, 'null', 0.0, 0.0, 3, -5, 711, 3, -5, 711, 0.0, 555.35, 3276, 0, 0, 0, 675.0]\n    print (a)\n    time.sleep(5)\n","output":"str","x":315.1999969482422,"y":2338.39990234375,"wires":[["e9a947dd9527f56c"]]},{"id":"e9a947dd9527f56c","type":"file","z":"0700cf57765c88b3","name":"","filename":"filename","filenameType":"msg","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":475.1999969482422,"y":2338.39990234375,"wires":[["615935d164ef5ad7"]]},{"id":"fb34d0c2bb89fcef","type":"debug","z":"0700cf57765c88b3","name":"msg1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":2320,"wires":[]},{"id":"e14263190fa389b6","type":"debug","z":"0700cf57765c88b3","name":"msg2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":2360,"wires":[]},{"id":"659f0ca4e541a56f","type":"debug","z":"0700cf57765c88b3","name":"msg3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":2400,"wires":[]},{"id":"615935d164ef5ad7","type":"exec","z":"0700cf57765c88b3","command":"python -u","addpay":"filename","append":"","useSpawn":"true","timer":"","winHide":false,"oldrc":false,"name":"","x":630,"y":2340,"wires":[["fb34d0c2bb89fcef"],["e14263190fa389b6"],["659f0ca4e541a56f"]]},{"id":"ac2eaf636142aa5b","type":"inject","z":"0700cf57765c88b3","name":"Stop","props":[{"p":"kill","v":"SIGTERM","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":490,"y":2380,"wires":[["615935d164ef5ad7"]]}]

I have the same problem on a raspberry with raspbian and on Windows 11 as well. Node-red V3.0.2

Can you show us the python and what you see in a debug node on the exec node please.

Aall the code you can test is in the code shared above. The python code is in the template node

I am on my phone so can't see the flow.
Show us what you see in a debug node.

The print code, that becames the message payload, is split into several messages, till even 5-6 messages, because the split point, so the payload message, has every time a different length, even if the print code is always the same.
Here the python code

#!/usr/bin/env python2.7
import time
while True:
    a=[402.17, 10.16, 17.2, 3482.43, 138.55, 27.76, 27.64, 0.29, 27.76, 3.52, 3.51, 3.68, 2.7, 4.15, 0.0, 8.98, 349.0, 592, 73, 189.77, 642.05, 8895.82, 7.19, 0, 0, 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 0.0, 0.0, 0, 2.83, 0, 0, 402.17, 10.16, 17.2, 402.17, 10.16, 0.34, 'null', 0.0, 0.0, 0.34, 0.0, 0.0, 0.0, 85.8, -4.2, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 67, 0, 0.0, 3.48, 0, 3.48, 56.8, 'null', 0.0, 0, 0, 41, 0, 0.0, 0.0, 0.0, 0.0, 5.75, 0.0, 85.51, 0.0, 0.0, 41, 0, 793, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 711, 3, -5, 3.48, 'null', 'null', 'null', 'null', 27.59, 27.59, 0.0, 0.0, 0, 0, 27.77, 27.75, 27.62, 0.0, 16.3, 16.3, 16.8, 16.8, 14.9, 7.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 555.37, 1406.65, 26.61, 26.61, 0.0, 'null', 'null', 'null', 'null', 0.0, 555.37, 0.0, 0.0, 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 0.0, 0.0, 7.9, 0.0, 0.0, 0.0, 8.66, 3482.43, 26.61, 325.98, 10.18, 27.56, 27.53, 27.76, 27.64, 396.96, 0.0, 0.0, 454.56, 'null', 0, 0, 0, 0, 0, 0, 'null', 0, 0, 0, 0, 0, 0, 'null', 0.0, 0.0, 3, -5, 711, 3, -5, 711, 0.0, 555.35, 3276, 0, 0, 0, 675.0]
    print (a)
    time.sleep(5)

Here you can see the debug nodes. This time the print code has been split into 4 separate messages, but its number varies, till 7 messages.


image

I have even noticed that the more times I use it, the more messages I gett for each print command. At the beginning they where just 3 messages, now they are more than 20

Is there any particular reason why you do this in python and not directly within node-red ?

Unfortunatly yes, I have to use python lenguage :frowning:
I simply reproduced the final result that I can later on handle in node-red without problem, but I cannot avoid to use python

You could add a newline character on the end of each print, then put the buffers back together in node red.

Yes, at the end this is what I'm doing but it shouldn't work like this, should it. Even because, as I wrote, I don't know why but the number of messages returned is incresing considerably

Has anyone else got any issues running the test flow, the OP's test flow runs fine on my debian and android node-red. 3.0.2, nodejs 14, no Docker

It runs fine as it is on Ubuntu, but on the pi it does produce random buffer sizes for some reason. It must be something to do with python on the pi, or the buffering in the OS. @Lupin_III try this in your template

#!/usr/bin/env python2.7
import time
import sys
while True:
    a=[402.17, 10.16, 17.2, 3482.43, 138.55, 27.76, 27.64, 0.29, 27.76, 3.52, 3.51, 3.68, 2.7, 4.15, 0.0, 8.98, 349.0, 592, 73, 189.77, 642.05, 8895.82, 7.19, 0, 0, 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 0.0, 0.0, 0, 2.83, 0, 0, 402.17, 10.16, 17.2, 402.17, 10.16, 0.34, 'null', 0.0, 0.0, 0.34, 0.0, 0.0, 0.0, 85.8, -4.2, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 67, 0, 0.0, 3.48, 0, 3.48, 56.8, 'null', 0.0, 0, 0, 41, 0, 0.0, 0.0, 0.0, 0.0, 5.75, 0.0, 85.51, 0.0, 0.0, 41, 0, 793, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 711, 3, -5, 3.48, 'null', 'null', 'null', 'null', 27.59, 27.59, 0.0, 0.0, 0, 0, 27.77, 27.75, 27.62, 0.0, 16.3, 16.3, 16.8, 16.8, 14.9, 7.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 555.37, 1406.65, 26.61, 26.61, 0.0, 'null', 'null', 'null', 'null', 0.0, 555.37, 0.0, 0.0, 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 0.0, 0.0, 7.9, 0.0, 0.0, 0.0, 8.66, 3482.43, 26.61, 325.98, 10.18, 27.56, 27.53, 27.76, 27.64, 396.96, 0.0, 0.0, 454.56, 'null', 0, 0, 0, 0, 0, 0, 'null', 0, 0, 0, 0, 0, 0, 'null', 0.0, 0.0, 3, -5, 711, 3, -5, 711, 0.0, 555.35, 3276, 0, 0, 0, 675.0]
    print (a)
    sys.stdout.flush()
    time.sleep(5)

I have added import sys at the front, and a buffer flush after the print. Also, In the exec node, remove the -u.

No, nothing. It's the same. Not only on the pi, even on Windows

Did you remember to remove the -u in the exec node?
If you did, can you post your new non-working flow please?

Python2.7 ??? Really ?

1 Like

THis is with the exec node without -u

[{"id":"8e82ee5ca7530af0","type":"tab","label":"Flow 4","disabled":false,"info":"","env":[]},{"id":"f24ff63822df1036","type":"inject","z":"8e82ee5ca7530af0","name":"","props":[{"p":"filename","v":"/home/pi/testing.py","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":140,"wires":[["d09fec707e868aef"]]},{"id":"d09fec707e868aef","type":"template","z":"8e82ee5ca7530af0","name":"Progress","field":"payload","fieldType":"msg","format":"python","syntax":"plain","template":"#!/usr/bin/env python2.7\nimport time\nwhile True:\n    a=[402.17, 10.16, 17.2, 3482.43, 138.55, 27.76, 27.64, 0.29, 27.76, 3.52, 3.51, 3.68, 2.7, 4.15, 0.0, 8.98, 349.0, 592, 73, 189.77, 642.05, 8895.82, 7.19, 0, 0, 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 0.0, 0.0, 0, 2.83, 0, 0, 402.17, 10.16, 17.2, 402.17, 10.16, 0.34, 'null', 0.0, 0.0, 0.34, 0.0, 0.0, 0.0, 85.8, -4.2, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 67, 0, 0.0, 3.48, 0, 3.48, 56.8, 'null', 0.0, 0, 0, 41, 0, 0.0, 0.0, 0.0, 0.0, 5.75, 0.0, 85.51, 0.0, 0.0, 41, 0, 793, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 711, 3, -5, 3.48, 'null', 'null', 'null', 'null', 27.59, 27.59, 0.0, 0.0, 0, 0, 27.77, 27.75, 27.62, 0.0, 16.3, 16.3, 16.8, 16.8, 14.9, 7.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 555.37, 1406.65, 26.61, 26.61, 0.0, 'null', 'null', 'null', 'null', 0.0, 555.37, 0.0, 0.0, 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 'null', 0.0, 0.0, 7.9, 0.0, 0.0, 0.0, 8.66, 3482.43, 26.61, 325.98, 10.18, 27.56, 27.53, 27.76, 27.64, 396.96, 0.0, 0.0, 454.56, 'null', 0, 0, 0, 0, 0, 0, 'null', 0, 0, 0, 0, 0, 0, 'null', 0.0, 0.0, 3, -5, 711, 3, -5, 711, 0.0, 555.35, 3276, 0, 0, 0, 675.0]\n    print (a)\n    time.sleep(5)\n","output":"str","x":310,"y":140,"wires":[["cca84906dfbc8f07"]]},{"id":"cca84906dfbc8f07","type":"file","z":"8e82ee5ca7530af0","name":"","filename":"filename","filenameType":"msg","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":470,"y":140,"wires":[["29959d933f062396"]]},{"id":"b564fa4a9c52c23f","type":"debug","z":"8e82ee5ca7530af0","name":"msg1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":844.8000030517578,"y":121.60009765625,"wires":[]},{"id":"c68ddaf00c486ef6","type":"debug","z":"8e82ee5ca7530af0","name":"msg2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":844.8000030517578,"y":161.60009765625,"wires":[]},{"id":"7a269500967ee829","type":"debug","z":"8e82ee5ca7530af0","name":"msg3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":844.8000030517578,"y":201.60009765625,"wires":[]},{"id":"29959d933f062396","type":"exec","z":"8e82ee5ca7530af0","command":"python","addpay":"filename","append":"","useSpawn":"true","timer":"","winHide":false,"oldrc":false,"name":"","x":614.8000030517578,"y":141.60009765625,"wires":[["b564fa4a9c52c23f"],["c68ddaf00c486ef6"],["7a269500967ee829"]]},{"id":"1cc06124eff8be96","type":"inject","z":"8e82ee5ca7530af0","name":"Stop","props":[{"p":"kill","v":"SIGTERM","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":484.8000030517578,"y":181.60009765625,"wires":[["29959d933f062396"]]}]

This time I get a single message per print command, but it is not complete. It is truncated at the end, and each message is truncated in a different way
And yes, unfortunatly it's a python 2

I suspect it is just that the debug node output is truncated in order to limit the load on the browser. Click the box in the node to send it to the console and check in the node red log, I think you will find it is all there.

On the python version, I think that the fact that you call it with the python command means that the #! line is ignored.