Skip to content

Commit

Permalink
- Run channel JSON loading multithreaded to speed up channel loading …
Browse files Browse the repository at this point in the history
…process
  • Loading branch information
nwithan8 committed Apr 9, 2022
1 parent 47b4747 commit 12aa9d0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
20 changes: 14 additions & 6 deletions dizqueTV/dizquetv.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import logging
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from typing import List, Union
from typing import List, Union, Dict
from xml.etree import ElementTree

import m3u8
Expand Down Expand Up @@ -467,6 +468,10 @@ def delete_plex_server(self, server_name: str) -> bool:

# Channels

def _get_channel_data(self, channel_number: int) -> Union[Dict, None]:
# large JSON may take longer, so bigger timeout
return self._get_json(endpoint=f'/channel/{channel_number}', timeout=5)

@property
def channels(self) -> List[Channel]:
"""
Expand All @@ -475,14 +480,17 @@ def channels(self) -> List[Channel]:
:return: List of Channel objects
:rtype: List[Channel]
"""
# temporary patch until /channels API is fixed. SLOW.
# temporary patch until /channels API is fixed. Runs concurrently to speed up.
numbers = self.channel_numbers
channels = []
for number in numbers:
json_data = self._get_json(endpoint=f'/channel/{number}', timeout=5)
# large JSON may take longer, so bigger timeout

channels_json_data = helpers._multithread(func=self._get_channel_data, elements=numbers,
element_param_name="channel_number")

for json_data in channels_json_data:
if json_data:
channels.append(Channel(data=json_data, dizque_instance=self))

return channels

def get_channel(self, channel_number: int = None, channel_name: str = None) -> Union[Channel, None]:
Expand Down Expand Up @@ -584,7 +592,7 @@ def lowest_available_channel_number(self) -> int:
:return: Int number of the lowest available channel
:rtype: int
"""
possible = range(1, self.highest_channel_number + 2) # between 1 and highest_channel_number + 1
possible = range(1, self.highest_channel_number + 2) # between 1 and highest_channel_number + 1
# find the lowest number of the differences in the sets
return min(set(possible) - set(self.channel_numbers))

Expand Down
30 changes: 30 additions & 0 deletions dizqueTV/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
import random
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
from datetime import datetime, timedelta
from typing import List, Union, Tuple

Expand All @@ -19,6 +20,35 @@


# Internal Helpers
def _multithread(func, elements: List, element_param_name: str, thread_count: int = 20, **kwargs) -> List:
"""
Multithread a function for elements in a list
:param func: Function to be multithreaded
:type func: function
:param elements: List of elements to be multithreaded
:type elements: list
:param element_param_name: Name of the parameter to be passed to the function
:type element_param_name: str
:param thread_count: Number of threads to use
:type thread_count: int, optional
:param kwargs: Keyword arguments to be passed to the function
:type kwargs: dict, optional
:return: List of results from the function
:rtype: list
"""
thread_list = []
pool = ThreadPoolExecutor(thread_count)

for element in elements:
temp_kwargs = kwargs.copy()
temp_kwargs[element_param_name] = element
thread_list.append(pool.submit(func, **temp_kwargs))

wait(thread_list, return_when=ALL_COMPLETED)
return [t.result() for t in thread_list]


def _combine_settings_add_new(new_settings_dict: dict,
default_dict: dict,
ignore_keys: List = None) -> dict:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_dizquetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def test_channel_count(self):
count = client().channel_count
assert type(count) == int

def test_channel_list(self):
channels = client().channels
assert type(channels) == list


class TestWithFakePlex:
def test_add_plex_server(self):
Expand Down

0 comments on commit 12aa9d0

Please sign in to comment.