Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added threadpool to WebsiteSource #344

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions lumen/sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ class WebsiteSource(Source):

source_type = 'live'

threads = param.Boolean(False, doc="Check the URLS asynchronous")

@cached_schema
def get_schema(self, table=None):
schema = {
Expand All @@ -673,16 +675,27 @@ def get_tables(self):

@cached(with_query=False)
def get(self, table, **query):
data = self._async_check() if self.threads else self._sync_check()
return pd.DataFrame(data)

def _check_live(self, url):
try:
return requests.get(url, timeout=10).ok
except Exception:
return False

def _sync_check(self):
data = []
for url in self.urls:
try:
r = requests.get(url)
live = r.status_code == 200
except Exception:
live = False
for url in self.urls.copy():
live = self._check_live(url)
data.append({"live": live, "url": url})
df = pd.DataFrame(data)
return df
return data

def _async_check(self):
urls = self.urls.copy()
with futures.ThreadPoolExecutor() as ex:
lives = ex.map(self._check_live, urls)
return [{"live": l, "url": u} for u, l in zip(urls, lives)]


class PanelSessionSource(Source):
Expand Down