One way to do a non-blocking wait is to use threading.Event:
import threading
dummy_event = threading.Event()
dummy_event.wait(timeout=1)
This can be set()
from another thread to indicate that something has completed. But if you are doing stuff in another thread, you could avoid the timeout and event altogether and just join
the other thread:
import threading
def create_the_file(completion_event):
# Do stuff to create the file
def Main():
worker = threading.Thread(target=create_the_file)
worker.start()
# We will stop here until the "create_the_file" function finishes
worker.join()
# Do stuff with the file
If you want an example of using events for more fine-grained control, I can show you that...
The threading approach won't work if your platform doesn't provide the threading module. For example, if you try to substitute the dummy_threading module, dummy_event.wait()
returns immediately. Not sure about the join()
approach.
If you are waiting for other processes to finish, you would be better off managing them from your own script using the subprocess module (and then, for example, using the wait
method to be sure the process is done before you do further work).
If you can't manage the subprocess from your script, but you know the PID, you can use the os.waitpid()
function. Beware of the OSError
if the process has already finished by the time you use this function...
If you want a cross-platform way to watch a directory to be notified of new files, I'd suggest using a GIO FileMonitor from PyGTK/PyGObject. You can get a monitor on a directory using the monitor_directory method of a GIO.File.
Quick sample code for a directory watch:
import gio
def directory_changed(monitor, file1, file2, evt_type):
print "Changed:", file1, file2, evt_type
gfile = gio.File(".")
monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
monitor.connect("changed", directory_changed)
import glib
ml = glib.MainLoop()
ml.run()