Код: Выделить всё
def collectAllDirectories():
fullDirList = []
w = walk(parent_dir, topdown=True)
# Get a complelte list of directories and files and add tem to a list
# Don't add them to the list if they have an omit exception.
for (dirpath) in w:
#print(dirpath[0])
return fullDirList
Код: Выделить всё
def doThingWithTimeout(dirpath):
global thread_finished
print(dirpath[0])
fullDirList.append(dirpath)
thread_finished.set() # Set the event to indicate that the thread has finished
def walkWithTimeout(parent_dir, timeout_per_iteration=30): # Default timeout per iteration is 30 sec
global thread_finished
w = walk(parent_dir, topdown=True)
for (dirpath) in w:
thread_finished.clear() # Clear the event before starting the thread
# Start a new thread for each iteration
t = threading.Thread(target=doThingWithTimeout, args=(dirpath,))
t.start()
# Wait for the thread to finish or timeout
thread_finished.wait(timeout_per_iteration)
if not thread_finished.is_set(): # If the thread has not finished within the timeout
print("Timed out while processing:", dirpath)
# Optionally handle the timeout here
continue # Move to the next directory
t.join() # Wait for the thread to finish before proceeding
Подробнее здесь: https://stackoverflow.com/questions/783 ... -walk-loop
Мобильная версия