Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from my_package.web.request import UnifiedRequestHandler
- from PIL import Image
- from io import BytesIO
- import os
- def process_image(image_bytes, save_path, format=None):
- try:
- image = Image.open(BytesIO(image_bytes))
- os.makedirs(os.path.dirname(save_path), exists_ok=True)
- image.save(save_path, format=format or image.format)
- print(f"Image saved to {save_path}")
- except Exception as e:
- print(f"Error processing image: {e}")
- def handle_image(image_bytes, idx):
- save_path = f"./images/image_{idx}.jpg"
- process_image(image_bytes, save_path)
- # Initialize the UnifiedRequestHandler with concurrency settings
- handler = UnifiedRequestHandler(range(5, 100, 10), 2.0)
- # range(min_workers, max_workers, max_joined_workers_per_interval), interval_in_seconds
- # List of URLs to download
- urls = ["https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"] * 1000
- # Create a processor for each image
- processors = [lambda image_bytes, idx=i: handle_image(image_bytes, idx) for i in range(len(urls))]
- # Fetch and process the imags using the list of processors
- handler.request_many(urls, async_mode=True).into(processors).await_()
- print(f"{handler.pool._min_workers}/{handler.current_size}/{handler.pool._max_workers}")
- print(f"(SEM?{handler.pool._idle_semaphore._value})")
- handler.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement