Skip to content

Commit

Permalink
Appendix | Update v1.1
Browse files Browse the repository at this point in the history
- Added Proper Sync Threaded Loading
- Improved Code Style of Async Wait Blocking
- Added Tests to check proper threading
  • Loading branch information
Vinyzu committed Dec 9, 2023
1 parent a816d10 commit 870ed7a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
21 changes: 15 additions & 6 deletions chrome_fingerprints/fingerprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ def __init__(self) -> None:
self.fingerprints: Optional[List[Dict[str, Any]]] = None

# Preloading: Fingerprint Decompression & Json Loading takes ~2 seconds
with ThreadPoolExecutor(max_workers=1) as executor:
self.fingerprint_loading_feature = executor.submit(self.initialize_fingerprints)
self.executor = ThreadPoolExecutor(max_workers=1)

try:
self.fingerprint_loading_feature = self.executor.submit(self.initialize_fingerprints)
except Exception as e:
self.executor.shutdown(wait=True, cancel_futures=True)
raise e

def initialize_fingerprints(self) -> None:
dir_path = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -74,10 +79,14 @@ def initialize_fingerprints(self) -> None:
self.fingerprints = orjson.loads(json_data)

def get_fingerprint(self, fingerprint_index: Optional[int] = None) -> ChromeFingerprint:
if not self.fingerprint_loading_feature.done():
self.fingerprint_loading_feature.result(timeout=10)
try:
if not self.fingerprint_loading_feature.done():
self.fingerprint_loading_feature.result(timeout=10)

assert self.fingerprints
assert self.fingerprints
except Exception as e:
self.executor.shutdown(wait=True, cancel_futures=True)
raise e

if fingerprint_index:
fingerprint = self.fingerprints[fingerprint_index]
Expand Down Expand Up @@ -109,7 +118,7 @@ async def initialize_fingerprints(self) -> None:

async def get_fingerprint(self, fingerprint_index: Optional[int] = None) -> ChromeFingerprint:
if not self.fingerprint_loading_feature.done():
await asyncio.wait([self.fingerprint_loading_feature])
await self.fingerprint_loading_feature

assert self.fingerprints

Expand Down
17 changes: 17 additions & 0 deletions tests/test_async_fingerprints.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import time

from chrome_fingerprints import AsyncFingerprintGenerator, ChromeFingerprint

Expand All @@ -20,3 +21,19 @@ async def test_all_fingerprints(async_fingerprint_generator: AsyncFingerprintGen
for index in range(10000):
chrome_fp = await async_fingerprint_generator.get_fingerprint(fingerprint_index=index)
assert isinstance(chrome_fp, ChromeFingerprint)


@pytest.mark.asyncio
async def test_threaded_loading():
start_time = time.time()
# Load the FingerprintGenerator
fingerprint_generator = AsyncFingerprintGenerator()
init_time = time.time() - start_time
# Load a Fingerprint
await fingerprint_generator.get_fingerprint()
load_time = time.time() - start_time

# Check if the Fingerprint Load Time is bigger than the Fingerprint Init Time
# Because the DataLoading is threaded, the Loading should Not Block until a fingerprint is requested
# Therefore, we can check the threaded loading by checking the Load Times.
assert load_time > init_time
16 changes: 16 additions & 0 deletions tests/test_fingerprints.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from chrome_fingerprints import FingerprintGenerator, ChromeFingerprint


Expand All @@ -15,3 +16,18 @@ def test_all_fingerprints(fingerprint_generator: FingerprintGenerator):
for index in range(10000):
chrome_fp = fingerprint_generator.get_fingerprint(fingerprint_index=index)
assert isinstance(chrome_fp, ChromeFingerprint)


def test_threaded_loading():
start_time = time.time()
# Load the FingerprintGenerator
fingerprint_generator = FingerprintGenerator()
init_time = time.time() - start_time
# Load a Fingerprint
fingerprint_generator.get_fingerprint()
load_time = time.time() - start_time

# Check if the Fingerprint Load Time is bigger than the Fingerprint Init Time
# Because the DataLoading is threaded, the Loading should Not Block until a fingerprint is requested
# Therefore, we can check the threaded loading by checking the Load Times.
assert load_time > init_time

0 comments on commit 870ed7a

Please sign in to comment.