使用多线程优化任务1加速获取弹幕

main
p6fxi93qh 2 months ago
parent 0c688527c0
commit 2bf66d6037

@ -0,0 +1,30 @@
from concurrent.futures import ThreadPoolExecutor
import os
import threading
from getdm import get_videos_url,get_danmu
def write_dm_to_file(video_id, file, lock):
danmu = get_danmu(video_id)
with lock:
for item in danmu:
file.write(f"{item}\n") # 每个弹幕单独一行
print(f"视频 {video_id} 的弹幕已写入")
def writedmtxt(file_path, res):
with open(file_path, 'w', encoding='utf-8') as file:
lock = threading.Lock()
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(write_dm_to_file, video_id, file, lock) for video_id in res]
for future in futures:
future.result() # 获取函数结果,这里可以用来处理异常
if __name__ == '__main__':
keyword = '2024巴黎奥运会'
res = get_videos_url(keyword, 10)
res_dir = os.path.join(os.getcwd(), 'res')
if not os.path.exists(res_dir):
os.makedirs(res_dir)
file_name = "total300_3.txt"
file_path = os.path.join(res_dir, file_name)
writedmtxt(file_path=file_path, res=res)
Loading…
Cancel
Save