Skip to content

Commit

Permalink
Use as_completed()
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Nov 5, 2023
1 parent ad4bde9 commit 9227124
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Services
"""

import concurrent.futures
import logging
import os
import re
from abc import ABC, abstractmethod
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from urllib.parse import urlparse, parse_qs
from typing import Any
Expand Down Expand Up @@ -203,14 +203,14 @@ def get_assigned_issues() -> list[Issue]:
def get_created_issues() -> list[Issue]:
return self.get_created(username, **kwargs)

with ThreadPoolExecutor(max_workers=2) as executor:
futures = []
futures = []
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
if assigned:
futures.append(executor.submit(get_assigned_issues))
if created:
futures.append(executor.submit(get_created_issues))
for future in futures:
issues.extend(future.result())
for future in concurrent.futures.as_completed(futures):
issues.extend(future.result())

return list(set(issues))

Expand All @@ -234,16 +234,16 @@ def get_issues(is_pr: bool, is_assigned: bool) -> list[Issue]:
return self.get_assigned(username, pull_requests=is_pr, **kwargs)
return self.get_created(username, pull_requests=is_pr, **kwargs)

with ThreadPoolExecutor(max_workers=4) as executor:
futures = []
futures = []
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
if assigned:
futures.append(executor.submit(get_issues, False, True))
futures.append(executor.submit(get_issues, True, True))
if created:
futures.append(executor.submit(get_issues, False, False))
futures.append(executor.submit(get_issues, True, False))
for future in futures:
issues.extend(future.result())
for future in concurrent.futures.as_completed(futures):
issues.extend(future.result())

return list(set(issues))

Expand Down Expand Up @@ -279,7 +279,9 @@ def get_issues(self, issues: list[dict]) -> list[Issue | None]:
"""
Multithreaded get_issues()
"""
with ThreadPoolExecutor(max_workers=min(10, len(issues))) as executor:
with concurrent.futures.ThreadPoolExecutor(
max_workers=min(10, len(issues))
) as executor:
return list(executor.map(lambda it: self.get_issue(**it), issues))


Expand Down

0 comments on commit 9227124

Please sign in to comment.