0

I have a python script:

//test.py

import psutil

while True:
    result = psutil.cpu_percent(interval=1)
    print(result)

and then nodejs code:

//test.js
    var PythonShell = require('python-shell')
    pyshell = new PythonShell('test.py')
    pyshell.on('message', function(message) {
        console.log(message)
    })

nothing happened when I executing node script. Please help me how to get data per second (like "real-time") from endless python code from Node and loggging it to console.

1 Answer 1

1

You need to flush STDOUT:

#test.py

import psutil
import sys

while True:
    result = psutil.cpu_percent(interval=1)
    print(result)
    sys.stdout.flush()

It looks like it's a common issue with the python-shell npm package - https://github.com/extrabacon/python-shell/issues/81

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.