wait until change

Thomas Heller theller at python.net
Fri Oct 17 18:49:40 EDT 2003


Peter Hansen <peter at engcorp.com> writes:

> Matthew Wilson wrote:
>> 
>> I just took a midterm in my operating systems class, and a big topic was
>> "busy waiting" and semaphores.
>> 
>> And now, today, a real-world example!  Anyway, the time.sleep() method
>> is probably good enough, but how would a semaphore solution work in
>> python?  A semaphore solution is one where the process that uses the
>> updated file is asleep until another process says "Hey! Wake up! I
>> updated the file!".  This avoids all those check_for_update() calls
>> which bog down the CPU.
>
> In this case, it appears the OP doesn't have the capability of changing
> the other application, so what you suggest is out.
>
> Dave's example with WMI uses the *operating system* to essentially
> signal semaphore-like that an update has been made.  As I suggested,
> it's obviously much more complicated than the simple time.sleep()
> plus os.stat() approach, but there are times when it's the right
> way to approach this problem.

Just for completeness, it should be mentioned that the win32 api
functions FindFirstChangeNotification and FindNextChangeNotification
could also be used.  They are wrapped in win32all, and probably could
also be made to work with ctypes.  Platform dependend, sure, but maybe a
much more lightweight approach than using wmi (and, for me at least,
probably better understandable).
>
> -Peter

Thomas




More information about the Python-list mailing list