0

What is the most efficient way to wait for a condition to be true before executing certain code? For example I want to execute code only if a file is modified. My code is within a 'white True:' loop. I'm not sure if this is the best thing to do or if this is common practice because the 'while True:' loop is constantly looping and using up the cpu. I can put a pause statement in there but I want to know what's the defacto way to do this? The script needs to be continuously running while waiting for the condition to be true.

while True:

    modTime = os.path.getmtime('C:\\example1.xlsx')

    if(modTime > curTime):

        #execute code
        #execute code
        #execute code
        curTime = modTime
superx
  • 157
  • 13
  • why not run it every 5 seconds or something? If you need actual continuous running, look into workers – SuperStew May 01 '18 at 21:26
  • 2
    It strongly depends on your needs. If you want efficient code, this is **not** the best solution out there (check [here](https://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes)). However, busy waiting can be an easy and useful solution if you are not going towards performances. You could surely add some `time.sleep(3)` to pause the process for 3 seconds before checking again. – Daniele Cappuccio May 01 '18 at 21:28
  • 1
    If you want to wait until a file is modified, don't wait and poll -- tell your operating system to notify you if and when it happens. On Linux, one sane way to do this is with [`incron`](http://inotify.aiken.cz/?section=incron&page=about&lang=en), which can start your script if-and-only-if the file being watched changes. – Charles Duffy May 01 '18 at 21:30
  • Frankly, I'd argue that the *general* case of the question is eligible for close-as-too-broad (since the answer will always depend on the specific event or condition one wants to wait for -- whether that be a GUI action or I/O activity or anything else -- and which notification-based APIs are available for the purpose), and the *specific* case of the question is eligible for close-as-duplicate. – Charles Duffy May 01 '18 at 21:33
  • @CharlesDuffy I agree I rather the OS notify me when a file is modified. But can this happen while my code is always running and be notified by the OS? – superx May 01 '18 at 21:43
  • 1
    @superx, yes, as the linked answer describes. Notification from the OS when a file changes while your code is running is a common requirement, which is why (OS-specific) frameworks implementing it are common -- how else would your IDE know to reload a source file when something else changes it, for example, or your file manager know to refresh to show that a file was added or deleted to the folder being shown? – Charles Duffy May 01 '18 at 21:46
  • @CharlesDuffy I will follow the lead here. Thanks. Do you recomment FindFirstChangeNotification API or watchdog as so many others has suggested. It appears that FindFirstChangeNotification API only notifies of directory changes not file. – superx May 01 '18 at 22:08
  • @DanieleCappuccio Thanks, If I can't find any other solutions i'd have to use time.sleep. But the links you provided me seems to be for directory changes. – superx May 01 '18 at 22:09
  • My relevant experience is Unix-flavored, so unfortunately I'm not able to recommend a specific API for the Windows side of the world. – Charles Duffy May 02 '18 at 00:47

0 Answers0