python "@" cause SyntaxError: invalid syntax

pyrapl:
https://pypi.org/project/pyRAPL/

import os
import pyRAPL

pyRAPL.setup() 
csv_output = pyRAPL.outputs.CSVOutput('result.csv')

command = "stress -c 4 -t 10"

@pyRAPL.measure(output=csv_output)

os.system("stress -c 4 -t 10")

csv_output.save()

python rapl_log.py

output:


  File "/home/edison/Downloads/rapl_log.py", line 11
    os.system("stress -c 4 -t 10")
    ^^
SyntaxError: invalid syntax

How can I fix this problem? I think that may caused by the "@" in "@pyRAPL.measure(output=csv_output)", but I am new to python~.

I hope to use pyrapl to log the energy value for run "stress".

>Solution :

The @ symbol is used for decorators, which modify functions, so you need to wrap your code in a function. Perhaps this modified version of your code would work:

Example slightly modified from the docs:

import pyRAPL

pyRAPL.setup()

csv_output = pyRAPL.outputs.CSVOutput('result.csv')

@pyRAPL.measure(output=csv_output)
def foo():
    # Instructions to be evaluated.
    os.system("stress -c 4 -t 10")

for _ in range(100):
    foo()

csv_output.save()

Leave a Reply