Hit a snag with Blynk python library script not looping

I"m been wanting to get my feet wet with some Blynk python library on my raspberry PI. I’m starting with this simple script to understand virtual pins but I hit snag with my code. I’m trying to display CPU load in Blynk widget but code only loops once and does not refresh CPU load. Blynk runs fine but not sure why my code does not loop. Any ideas what I’m missing? Thank you in advance.

    import BlynkLib
    import subprocess
    import time

    BLYNK_AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
    blynk = BlynkLib.Blynk(BLYNK_AUTH, '192.xxx.x.xx', 8080)


    while True:
     cmd = "top -bn1 | grep load | awk '{printf \"%.2f\", $(NF-2)}'"
     CPU = subprocess.check_output(cmd, shell = True )

     print(CPU)

     @blynk.VIRTUAL_READ(12)
     def my_read_handler():
         blynk.virtual_write(12, CPU)
     time.sleep(1)

     blynk.run()

If you plug your code into python idle, do you get what you’re looking for?

import subprocess

  while True:
     cmd = "top -bn1 | grep load | awk '{printf \"%.2f\", $(NF-2)}'"
     CPU = subprocess.check_output(cmd, shell = True )
print(CPU)

I’ll check and get back to you but keep in mind that the script works and prints CPU load fine at least once and comes up on widget and it just does not loop/refresh. It appears as if Blynk is preventing the loop, though not sure why.

is blocking on Python. You can set up all virtual pin handlers ans then only call Blynk.run() once
it is indeed a thing we should change, but in any case it won’t work as “on Arduinos”

@malvar Try this way… you can set a timer using blynk.set_user_task() But I have yet to get multiple tasks to work… so not sure if it is possible.

Meanwhile, this way does work… but I don’t know what the resultant number references, as it is not the same as the CPU percentage on the RPi taskbar.

Your display widget needs to be set to PUSH

import BlynkLib
import subprocess
import time

BLYNK_AUTH = '5fb55413762b400cbb979111b630b9af'
blynk = BlynkLib.Blynk(BLYNK_AUTH, '192.xxx.x.xx', 8080)

cmd = "top -bn1 | grep load | awk '{printf \"%.2f\", $(NF-2)}'"


# This task will read then display the result of the above cmd.
@blynk.VIRTUAL_WRITE(12)
def my_read_handler():
    CPU = subprocess.check_output(cmd, shell = True )
    blynk.virtual_write(12, CPU)
    print(CPU)



# This command will run the defined task every 1000ms (1 second).
blynk.set_user_task(my_read_handler, 1000)



blynk.run()

I was really excited for a moment because the timer did work, and so I started incorporating this code into my code which interacts with sensors and other data reporting hardware…but then I realized what you meant by
“But I have yet to get multiple tasks to work” as that is what happened to me also. It would be really awesome if someone could figure out how to run multiple tasks. Thank you gunner for the help.

2 Likes

I would really love to know if anybody has figured out a way to run multiple tasks with the python Blynk library. This would make it a lot easier for me to run all my python code with Blynk instead of NodeJS.
Thanks in advance.

Only @vshymanskyy will know if/how/why blynk.set_user_task() is only good for ONE task :stuck_out_tongue_winking_eye: seems a bit silly to me.

Meanwhile I have had some limited results with multiple threads… probably doing it wrong :smile: but it seems to do the job when testing… this is an additional counter I have running in my testing script…

import thread

# Counter Display
def counter_task(cntr):
    while cntr > 0:
        blynk.virtual_write(4,cntr)
        #print (cntr)
        cntr += 1
        time.sleep(5)

thread.start_new_thread(counter_task, (1,))

So far I have three different timing methods… reading frequency on a widget, blynk_user_task() and thread… an ungainly combination to say the least :blush:

1 Like

@Gunner due to historical reasons :wink:
Python itself did not provide any good way to handle multiple tasks in parallel. So this is what we have.
Now we have threads, but it will require rewriting the library - and I’m not going to do that.

Also, as a baby step, I think I’ll make run() non-blocking - so users can handle their tasks in a loop (like in Arduino).

1 Like

Here is a template of the way I run Blynk multi-threaded in Python. You might want to add it to your repository as I see there are a number of forum threads asking the same question on how to do this.