rbonghi / jetson_stats

📊 Simple package for monitoring and control your NVIDIA Jetson [Orin, Xavier, Nano, TX] series
https://rnext.it/jetson_stats
GNU Affero General Public License v3.0
2.14k stars 261 forks source link

Jetson stats not working with python subprocess module #246

Closed harshad-0302 closed 1 year ago

harshad-0302 commented 2 years ago

I have to run my application binary and get the jtop data into a csv file while running the app. So I have written this code to do the same . But the issue is that when I am running it the script automatically exits in sometime and it is not running for the duration given. Sharing the code below:

from jtop import jtop

import csv

import subprocess

import sys

""" Function to export jtop data to csv file """ def export_data(): print("Exporting jtop data to csv")

with jtop() as jetson:
    # boards
    print('*** board ***')
    print(jetson.board)
    print('Execution time 60sec approx')

    csvfile = open('Output_data.csv','w') 

    writer = csv.DictWriter(csvfile,fieldnames=jetson.stats.keys()) 
    writer.writeheader()
    writer.writerow(jetson.stats)
    start_time=jetson.stats['time']

    # jetson.ok() will provide the proper update frequency
    while jetson.ok():

        curr_time=jetson.stats['time']
        if(str(curr_time-start_time) >= '0:00:90.620888'):
            print(str(curr_time-start_time))
            print("Script_Exit")
            break
        else:
            print("Script_Running")
            print(str(curr_time-start_time))
            writer.writerow(jetson.stats)  

csvfile.close()    

""" Function to execute binary and export function using subprocess module """ def Execute_bin(): command="./My_App.bin" processWorkingDir="....../path/"

process = subprocess.Popen(command, cwd=processWorkingDir )  

print("\n##############################################################\n")
export_data()
print("\n##############################################################\n")
process.kill()

if name == "main":

Execute_bin()

EOF

If I am running the application binary and jetson stats python script seperatly in two different terminals then it is working fine . But while running in one terminal using subprocess it exits automatically after sometime. Not getting where is the issue.

rbonghi commented 1 year ago

Hi @harshad-0302 ,

I tested your code, and I didn't see any errors, I just made a few changes to your code. I suggest (like you are already well using) the function jetson.ok() to run before reading the data. I am still releasing a new jetson_stats release and should fix many bugs and issues with python 3.x I suggest reusing on your project :-)

from jtop import jtop
import csv

csvfile = open('Output_data.csv','w') 

with jtop() as jetson:
    # boards
    if jetson.ok():
        print('*** board ***')
        print(jetson.board)
        print('Execution time 60sec approx')

        writer = csv.DictWriter(csvfile,fieldnames=jetson.stats.keys()) 
        writer.writeheader()
        writer.writerow(jetson.stats)
        start_time=jetson.stats['time']

        # jetson.ok() will provide the proper update frequency
        while jetson.ok():

            curr_time=jetson.stats['time']
            if(str(curr_time-start_time) >= '0:00:90.620888'):
                print(str(curr_time-start_time))
                print("Script_Exit")
                break
            else:
                print("Script_Running")
                print(str(curr_time-start_time))
                writer.writerow(jetson.stats)  

csvfile.close()