Как добавить тайм-ауты для каждой итерации внутри цикла Python os.walk?Linux

Ответить
Anonymous
 Как добавить тайм-ауты для каждой итерации внутри цикла Python os.walk?

Сообщение Anonymous »

У меня есть следующее определение, которое содержит статистику всех каталогов на сервере:

Код: Выделить всё

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
Но, похоже, он никогда не достигал порога тайм-аута, несмотря на то, что каталог был указан более 30 секунд. Я новичок в многопоточности, поэтому уверен, что делаю что-то не так (и я использовал ИИ, чтобы помочь с вышеизложенным). Кто-нибудь знает, как добавить таймауты для каждого каталога внутри цикла os.walk?

Подробнее здесь: https://stackoverflow.com/questions/783 ... -walk-loop
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Linux»