From 73e29138cc1c9266c7fa133f21b042eb21005b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Buk?= Date: Wed, 18 May 2022 11:14:39 +0200 Subject: [PATCH 1/2] Uncomment buffer clear --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 5f0c04e..8385941 100644 --- a/app/main.py +++ b/app/main.py @@ -54,7 +54,7 @@ def load(payload: CheckRequest, _: None = Depends(verify_api_key)): break else: pattern_index = 0 - # Buffer.clear() + Buffer.clear() return CheckResponse(exists=exists) From 5e6f25b1d7f8709236f82cfe140eeacc86f419de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Buk?= Date: Wed, 18 May 2022 12:28:59 +0200 Subject: [PATCH 2/2] Initial implementation of parallelisation --- app/main.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index 8385941..e7d2bab 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,7 @@ import uvicorn + +from threading import Thread +from queue import Queue from buffer import Buffer from os import environ @@ -38,22 +41,38 @@ def check(payload: LoadRequest, _: None = Depends(verify_api_key)): @app.post(f"/check", response_model=CheckResponse) def load(payload: CheckRequest, _: None = Depends(verify_api_key)): + def check_buffer(buffer, output_queue): + pattern_index = 0 + exists = False + for number in buffer: + if number == payload.pattern[pattern_index] or payload.pattern[pattern_index] is None: + pattern_index += 1 + if pattern_index == len(payload.pattern): + exists = True + break + else: + pattern_index = 0 + output_queue.put(exists) + if len(Buffer.data) == 0: raise HTTPException(400, {"reason": "Buffer is empty"}) if len(payload.pattern) == 0: raise HTTPException(400, {"reason": "Pattern cannot be empty"}) if len(payload.pattern) > MAX_LENGTH: raise HTTPException(413, {"reason": f"Pattern cannot be longer than {MAX_LENGTH}"}) - pattern_index = 0 + + threads = [] + results = Queue() exists = False - for number in Buffer.data: - if number == payload.pattern[pattern_index] or payload.pattern[pattern_index] is None: - pattern_index += 1 - if pattern_index == len(payload.pattern): - exists = True - break - else: - pattern_index = 0 + chunk_size = 50 + for i in range(max(len(Buffer.data) // chunk_size, 1)): + thread = Thread(target=check_buffer, args=(Buffer.data[i*chunk_size:i*chunk_size + chunk_size*2], results), daemon=True) + thread.start() + threads.append(thread) + for _ in range(len(threads)): + if results.get(): + exists = True + break Buffer.clear() return CheckResponse(exists=exists)