B44ken's solution to "Python Script to Run Command N Times a Second"

Super simple, but works flawlessly.<\/p>\n

import<\/span> os<\/span>,<\/span> time<\/span>\ndef<\/span> do_loop<\/span>(<\/span>hz<\/span>,<\/span> dofor<\/span>,<\/span> cmd<\/span>):<\/span>\n    times_to_run<\/span> =<\/span> hz<\/span> *<\/span> dofor<\/span>\n    for<\/span> i<\/span> in<\/span> range<\/span>(<\/span>times_to_run<\/span>):<\/span>\n        time<\/span>.<\/span>sleep<\/span>(<\/span>1<\/span>/<\/span>hz<\/span>)<\/span>\n        os<\/span>.<\/span>system<\/span>(<\/span>cmd<\/span>)<\/span>\n\n# do_loop(5, 1, &quot;echo hi&quot;)<\/span>\n<\/pre><\/div>
Super simple, works flawlessly. I wrote this on Windows, but I'm sure it works flawlessly. ```python importthe same with bash as it does for batch. ``` import os, time def do_loop(hz, dofor, cmd): outputs = [] times_to_run = hz * dofor for i in range(times_to_run): time.sleep(1/hz) code = os.system(cmd) # ) outputs += [code] print(f"done! return codes: {outputs}") # do_loop(5, 1, "echo hi") ```

User: B44ken

Question: Python Script to Run Command N Times a Second

Back to question