Skip to content

How to turn system monitor script into a app? #2245

Answered by marek-mihok
HugoP asked this question in Q&A
Discussion options

You must be logged in to vote

The answer lies in unblocking psutil.cpu_percent() by removing its interval=1 parameter and using asynchronous q.sleep instead of synchronous time.sleep. You can see it's non-blocking on this counter app:

Screen.Recording.2024-03-20.at.17.22.37.mov
import psutil

from h2o_wave import main, app, Q, ui, data

async def monitor_cpu(q: Q):
    while True:
        await q.sleep(1)
        cpu_usage = psutil.cpu_percent()
        q.page['monitor'].value = cpu_usage
        q.page['monitor'].data.usage = cpu_usage
        q.page['monitor'].plot_data[-1] = [q.client.tick, cpu_usage]
        q.client.tick += 1    
        await q.page.save()

@app('/monitor')
async def serve(q: Q):
    if n…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by HugoP
Comment options

You must be logged in to vote
2 replies
@HugoP
Comment options

@mturoci
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants