You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python线程池运行函数阻塞问题及获取下载文件名需求

问题原因

你的代码核心问题出在output.result()这个调用上——它是阻塞式的。主线程会暂停在这里,一直等待downloadedfiledetector函数执行完毕才会继续往下运行下载代码。但downloadedfiledetector本身在等待新文件出现,这就形成了死循环:检测函数等下载触发,主线程等检测函数结束,下载代码永远没机会执行。

修正方案

调整线程执行顺序:先启动检测线程,不立即获取结果,而是先执行下载代码,等下载操作触发文件生成后,再去获取检测线程的返回值。同时修复代码里的其他逻辑瑕疵。

修正后的完整代码

import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import undetected_chromedriver as bypass
import concurrent.futures

def downloadedfiledetector(scannedfolder):
    seconds = 0
    dl_wait = True
    # 初始化初始文件列表,排除Chrome临时下载文件
    initial_files = set(fname for fname in os.listdir(scannedfolder) if not fname.endswith('.crdownload'))
    
    while dl_wait and seconds < 120:
        time.sleep(1)
        current_files = set(fname for fname in os.listdir(scannedfolder) if not fname.endswith('.crdownload'))
        # 找出新增的文件
        new_files = current_files - initial_files
        if new_files:
            dl_wait = False
            # 返回第一个新增的完整文件路径
            return os.path.join(scannedfolder, next(iter(new_files)))
        seconds += 1
    return None  # 超时返回None

# 启动线程池,提交检测任务
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
    future = executor.submit(downloadedfiledetector, cliplocation)
    
    # 主线程执行下载流程
    driver = bypass.Chrome(options=options)
    driver.get(subtitleurl)
    
    WebDriverWait(driver, 300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#root > section > section.container.mx-auto.py-6.mt-4.sm\:mt-6 > div.mx-auto.max-w-4xl.leading-normal.mt-6.sm\:mt-10.sm\:flex.justify-center.px-4.lg\:px-0.text-center > div > div > div > input')))
    enterurlbox = driver.find_element(By.CSS_SELECTOR, '#root > section > section.container.mx-auto.py-6.mt-4.sm\:mt-6 > div.mx-auto.max-w-4xl.leading-normal.mt-6.sm\:mt-10.sm\:flex.justify-center.px-4.lg\:px-0.text-center > div > div > div > input')
    enterurlbox.send_keys(youtubeurl)
    
    WebDriverWait(driver, 300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#root > section > section.container.mx-auto.py-6.mt-4.sm\:mt-6 > div.mx-auto.max-w-4xl.leading-normal.mt-6.sm\:mt-10.sm\:flex.justify-center.px-4.lg\:px-0.text-center > button')))
    downloadbutton = driver.find_element(By.CSS_SELECTOR, '#root > section > section.container.mx-auto.py-6.mt-4.sm\:mt-6 > div.mx-auto.max-w-4xl.leading-normal.mt-6.sm\:mt-10.sm\:flex.justify-center.px-4.lg\:px-0.text-center > button')
    downloadbutton.click()
    
    WebDriverWait(driver, 300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#root > section > main > section.max-w-3xl.mx-auto.md\:flex.md\:justify-between.pt-4 > div.w-full.px-4.lg\:px-0.mb-6.md\:mb-0 > ul > li.py-6.text-center.block.w-full.px-2.sm\:px-4.sm\:py-3.sm\:flex.justify-between.items-center.border-b.border-gray-200.dark\:border-night-500 > section > div > div > a:nth-child(1)')))
    srtdownloadbutton = driver.find_element(By.CSS_SELECTOR, '#root > section > main > section.max-w-3xl.mx-auto.md\:flex.md\:justify-between.pt-4 > div.w-full.px-4.lg\:px-0.mb-6.md\:mb-0 > ul > li.py-6.text-center.block.w-full.px-2.sm\:px-4.sm\:py-3.sm\:flex.justify-between.items-center.border-b.border-gray-200.dark\:border-night-500 > section > div > div > a:nth-child(1)')
    srtdownloadbutton.click()
    
    # 等待检测线程返回结果
    filenamesubtitle = future.result()
    driver.close()

# 执行字幕合并操作
if filenamesubtitle:
    combinesubtitletovideo(filenamesubtitle)
else:
    print("下载超时,未检测到新的字幕文件")

关键修改点

  • 移除提前调用的result(),让检测线程在后台并行运行,主线程优先执行下载代码
  • 简化检测函数逻辑,直接对比初始与当前文件列表,更高效准确
  • 调整driver关闭时机,避免检测线程提前关闭主线程创建的浏览器实例
  • 增加超时处理,防止无限等待
  • 补全原代码缺失的WebDriverWait和EC模块导入

内容的提问来源于stack exchange,提问作者user20839568

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 01:50:33