0

I have a loop and when the condition excel_active() is true, it is an endless loop, and always try to read a .txt file and then execute it and then erase the content in .txt. It eats a lot of CPU when running.

What I want to ask is that is there any way to save the CPU besides time.sleep? Something like Watchdog (current version 0.9.0) in Linux? I am not familiar with Watchdog, can anyone give me some detailed suggestions?

    while excel_active():
    #time.sleep(0.5)
    try:        
        with open(masterpath, 'r') as f:
            s = f.read()
        exec(s)
        with open(masterpath,'w') as g:
            g.truncate()            
    except: 
        pass

I really appreciate anyone's help!

Usman
  • 1,983
  • 15
  • 28
Andy
  • 101
  • 1
  • 1
  • 7
  • 1
    Why `truncate`? You could open it with `w` mode and just close it afterwards. Faster, I suppose. – DirtyBit Mar 18 '19 at 09:46
  • watchdog works on windows, why do you think it's restricted to linux? – buran Mar 18 '19 at 09:47
  • 1
    `exec` is highly dangerous and should NOT be used on untrusted inputs, and bare except clauses should be forbidden in production code (specially when followed by a `pass`). – bruno desthuilliers Mar 18 '19 at 09:50
  • Possible duplicate of [How do I watch a file for changes?](https://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes) – Lanting Mar 18 '19 at 10:04

0 Answers0