[Tutor] program that 'waits' for file?

Kent Johnson kent37 at tds.net
Sun Aug 27 18:39:46 CEST 2006


Kay White wrote:
> Hello,
>
> I'm trying to make a program that will wait for a specific file to be 
> made by another program, and when that happens it opens and modifies 
> that file.
>
> I want my program to run "behind the scenes" so to speak. The user 
> will start it, enter the highest numbered screenshot they have, and 
> then start the game. When they take a new screenshot ingame, the 
> resulting file will be made into a texture on a 3d model that will 
> appear in the game world.
>
> I think I can handle the image processing and file writing functions 
> using PIL, and the ingame model is no problem. But I don't know how to 
> make my program "wait" for the file to be created. Is this possible, 
> and if so, can someone show me how to do it?
>
It might be enough to have a loop that calls time.sleep() to delay. For 
example,

import time
while True:
  if new_image_available():
    process_image()
  time.sleep(1)

The best timeout is a balance between responsiveness (shorter timeout is 
more responsive) and CPU usage (longer timeout uses less CPU polling for 
files).

This will work if you can either program an exit to the loop (e.g. after 
processing some number of images) or it's OK to kill the program 
externally (e.g. from Windows Task Manager). If you want to be able to 
kill the program from inside the same program it is harder, you need 
some kind of threading.

Kent
>
> ------------------------------------------------------------------------
> Get your own web address for just $1.99/1st yr 
> <%20http://us.rd.yahoo.com/evt=43290/*http://smallbusiness.yahoo.com/domains>. 
> We'll help. Yahoo! Small Business 
> <http://us.rd.yahoo.com/evt=41244/*http://smallbusiness.yahoo.com/>.
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   




More information about the Tutor mailing list