-
-
Save kj-9/03130d29292660d1a2cc4b74589ea133 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # benchmarking: | |
| # ```sh | |
| # hyperfine --runs 3 --prepare 'rm -rf output/*' -L python 3.13,3.13t,3.14,3.14t -L type sync,thread,async,async-thread \ | |
| # 'uv run --no-config --python {python} conncurrent-yt-dlp.py --type {type} --num-threads 3 --tasks 3' | |
| # ``` | |
| # /// script | |
| # dependencies = [ | |
| # "yt-dlp" | |
| # ] | |
| # /// | |
| import argparse | |
| import time | |
| import asyncio | |
| from pathlib import Path | |
| from concurrent.futures import ThreadPoolExecutor | |
| import yt_dlp | |
| # worst quality video with no audio | |
| def get_options(file_path: Path): | |
| return { | |
| "format": "worstvideo", | |
| "quiet": True, | |
| "outtmpl": str(file_path), | |
| } | |
| def download_video(video_id: str, file_path: Path): | |
| with yt_dlp.YoutubeDL(get_options(file_path)) as ydl: | |
| ydl.download([video_id]) | |
| async def async_download_video(video_id: str, file_path: Path, max_workers=None): | |
| loop = asyncio.get_running_loop() | |
| loop.set_default_executor(ThreadPoolExecutor(max_workers=max_workers)) | |
| download_video(video_id, file_path) | |
| async def async_download_video_thread(video_id: str, file_path: Path, max_workers=None): | |
| loop = asyncio.get_running_loop() | |
| loop.set_default_executor(ThreadPoolExecutor(max_workers=max_workers)) | |
| with yt_dlp.YoutubeDL(get_options(file_path)) as ydl: | |
| await asyncio.to_thread(ydl.download, [video_id]) | |
| async def main(): | |
| parser = argparse.ArgumentParser(description="Run a CPU-bound task with processes") | |
| parser.add_argument( | |
| "--type", | |
| type=str, | |
| default="process", | |
| choices=["sync", "thread", "async", "async-thread"], | |
| help="Type of executor: process or thread", | |
| ) | |
| parser.add_argument("--num-threads", type=int, default=4, help="Number of threads") | |
| parser.add_argument("--tasks", type=int, default=10, help="Number of tasks") | |
| args = parser.parse_args() | |
| print(f"Running {args.tasks} tasks with {args.num_threads} {args.type}s") | |
| output_dir = Path(__file__).parent / "output" / args.type | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| # https://www.youtube.com/watch?v=v9Qiwzgkxhw | |
| video_ids = ["v9Qiwzgkxhw"] * args.tasks | |
| start_time = time.time() | |
| if args.type == "sync": | |
| for i in range(args.tasks): | |
| download_video(video_ids[i], output_dir / f"video-{i}.mp4") | |
| elif args.type == "thread": | |
| executor = ThreadPoolExecutor(max_workers=args.num_threads) | |
| with executor: | |
| list( | |
| executor.map( | |
| download_video, | |
| video_ids, | |
| [output_dir / f"video-{i}.mp4" for i in range(args.tasks)], | |
| ) | |
| ) | |
| elif args.type == "async": | |
| tasks = [ | |
| async_download_video( | |
| "v9Qiwzgkxhw", output_dir / f"video-{i}.mp4", args.num_threads | |
| ) | |
| for i in range(args.tasks) | |
| ] | |
| await asyncio.gather(*tasks) | |
| elif args.type == "async-thread": | |
| tasks = [ | |
| async_download_video_thread( | |
| "v9Qiwzgkxhw", output_dir / f"video-{i}.mp4", args.num_threads | |
| ) | |
| for i in range(args.tasks) | |
| ] | |
| await asyncio.gather(*tasks) | |
| else: | |
| raise ValueError( | |
| f"Type must be sync, thread, async, or async-thread, not {args.type}" | |
| ) | |
| end_time = time.time() | |
| duration = end_time - start_time | |
| print(f"Time with {args.type}s: {duration:.2f} seconds") | |
| if __name__ == "__main__": | |
| asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment