@swyt

Почему многопоточность зависает после выполнения всех запросов?

Работа программы не завершается после выполнения всех запросов, если во время запроса хотя бы один из них получил отрицательные ответ или не дождался его. Добавления timeout в запрос не помогло. Почему это происходит?
import requests
import time
import os
import sys
from threading import Thread, current_thread
from queue import Queue

theard_count = 40

def check_url(url):
    headers = {'xxxxxxxxx': 'xxxxx'}
    res = requests.get(f'{url})', headers=headers, timeout=2)
    if res.status_code == 200: return True
    else: return False

def run(queue, result_queue):
    while not queue.empty():
        url = queue.get_nowait()
        print(f'{url} checking in thread {current_thread()}')
        status = check_url(url)
        result_queue.put_nowait((status, url))
        queue.task_done()
        print(f'{url} finished in thread {current_thread()}. Result={status}')
    print(f'{current_thread()} closing')

def main():
    start_time = time.time()
    queue = Queue()
    result_queue = Queue()
    url = ['Список ссылок']
    for j in url:
        queue.put(j)
    for i in range(theard_count):
        thread = Thread(target=run, args=(queue, result_queue)
        thread.daemon = True
        thread.start()
    queue.join()
    print(time.time() - start_time)

if __name__ == '__main__':
    main()
  • Вопрос задан
  • 103 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы