404background / node-red-contrib-python-venv

Node for python virtual environment
MIT License
9 stars 3 forks source link

Fpm/continuous script #20

Closed FrederikPM closed 2 months ago

FrederikPM commented 3 months ago

Implemented a continuous mode inspired by the feature found in the pythonshell node

However, this continuous mode supports being triggered not only by the button on the node but also by any input to the node. Thus, if continuous mode is enabled, the following applies:

404background commented 2 months ago

Thank you @FrederikPM ! Thanks also for adding Eslint and Prettier. I have never used these tools, so it may take me some time to check the code.

I have checked that if it is not in continuous mode, this node runs as before. image image

If continuous mode is set, a new input to the node terminates the already running Python process and starts it again. image

When I press the button on the node, it stops executing and when I press it again, it starts from the beginning. image

If msg.terminate is true, the process is terminated, and if false, the process is executed again. image

If "Continuously running script terminated" is displayed, pressing the button on the node executes the script again, but if it is not displayed, pressing the button does not execute the script.

Is this the expected behavior? Below is the flow of checking the behavior of continuous mode.

``` [{"id":"bf5e9a74624910c7","type":"inject","z":"3a0260d17167ea2b","name":"msg.terminate","props":[{"p":"terminate","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","_mcu":{"mcu":false},"x":1250,"y":920,"wires":[["4c7b5c32b5044e94"]]},{"id":"4c7b5c32b5044e94","type":"venv","z":"3a0260d17167ea2b","venvconfig":"e6a1da31b298e21a","name":"msg property","code":"print('start')\n\nimport time\ntime.sleep(3)\n\nprint('end')","continuous":true,"_mcu":{"mcu":false},"x":1450,"y":880,"wires":[["98387e89aa91bb21"]]},{"id":"98387e89aa91bb21","type":"debug","z":"3a0260d17167ea2b","name":"debug 97","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","_mcu":{"mcu":false},"x":1620,"y":880,"wires":[]},{"id":"badade14abfd97d3","type":"inject","z":"3a0260d17167ea2b","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","_mcu":{"mcu":false},"x":1270,"y":880,"wires":[["4c7b5c32b5044e94"]]},{"id":"e6a1da31b298e21a","type":"venv-config","venvname":"test","version":"3.8","_mcu":{"mcu":false}}] ```
FrederikPM commented 2 months ago

Good catch! Here is a python script with a while loop for the continuous mode:

[{        "id": "25441caf3c8534e2",        "type": "venv",        "z": "7a7ae303f662be9f",        "venvconfig": "4657b6fbdbaf6f7e",        "name": "venv test",        "code": "# Import the time library\nimport time\nstart = time.time()\n\noffset = msg[\"payload\"] if \"payload\" in msg else 0\n\ntime.sleep(1)\nend = time.time()\nlength = end - start\nprint(f'Time: {length}')\n\nwhile True:\n    time.sleep(1)\n    end = time.time()\n    length = end - start\n    print(f'Time: {length + offset}')",        "continuous": true,        "stdInData": false,        "x": 380,        "y": 180,        "wires": [            [                "114b918e3eb7ad96"            ]        ]    },    {        "id": "446c4d54c3afdb80",        "type": "inject",        "z": "7a7ae303f662be9f",        "name": "",        "props": [            {                "p": "payload"            }        ],        "repeat": "",        "crontab": "",        "once": false,        "onceDelay": 0.1,        "topic": "",        "payload": "5",        "payloadType": "num",        "x": 110,        "y": 220,        "wires": [            [                "25441caf3c8534e2"            ]        ]    },    {        "id": "5056f4320a8479d0",        "type": "inject",        "z": "7a7ae303f662be9f",        "name": "terminate",        "props": [            {                "p": "terminate",                "v": "true",                "vt": "bool"            }        ],        "repeat": "",        "crontab": "",        "once": false,        "onceDelay": 0.1,        "topic": "",        "x": 120,        "y": 140,        "wires": [            [                "25441caf3c8534e2"            ]        ]    },    {        "id": "114b918e3eb7ad96",        "type": "debug",        "z": "7a7ae303f662be9f",        "name": "debug 1",        "active": true,        "tosidebar": true,        "console": false,        "tostatus": false,        "complete": "payload",        "targetType": "msg",        "statusVal": "",        "statusType": "auto",        "x": 640,        "y": 180,        "wires": []    },    {        "id": "4657b6fbdbaf6f7e",        "type": "venv-config",        "venvname": "pyenv",        "version": "default"    }]

In regards to eslint and prettier it was simply to have a concise coding style for the project when using vscode. Feel free to change the rules as you see fit (can be done in .prettierrc) :)

404background commented 2 months ago

Thank you @FrederikPM ! I have checked that a python script with a while loop works in continuous mode. I will add a sample flow later. image

I have also checked that the code can be formatted with Prettier. I have been concerned about unifying the writing style, so I will use it!

I merge this Pull Request. If you see any behavior that you do not expect, please feel free to submit a Pull Request.