From d5081f6cd835c6fb42b88ac1f0cd719e802c1002 Mon Sep 17 00:00:00 2001 From: Julius Freudenberger Date: Mon, 1 May 2023 11:57:33 +0200 Subject: [PATCH] Add multithreading per attraction --- main.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 4d27de8..23c7050 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,13 @@ +from concurrent.futures import ThreadPoolExecutor + from csv_exporter import export_attraction -from tripadvisor_attraction import TripadvisorAttraction from tripadvisor_parser import TripadvisorAttractionParser -def parse_locations(urls: [str]) -> [TripadvisorAttraction]: - attractions = [] - for url in urls: - attractions.append(parse_location(url)) - - return attractions - - -def parse_location(url: str) -> TripadvisorAttraction: +def parse_and_export_attraction(url: str): parser = TripadvisorAttractionParser(url.strip()) - return parser.parse() + attraction = parser.parse() + export_attraction(attraction) def get_urls() -> [str]: @@ -22,9 +16,8 @@ def get_urls() -> [str]: def main(): - attractions = parse_locations(get_urls()) - for attraction in attractions: - export_attraction(attraction) + with ThreadPoolExecutor() as executor: + executor.map(parse_and_export_attraction, get_urls()) if __name__ == "__main__":