From cb7cc145298880355557e02201c09e8a9bcaa6c6 Mon Sep 17 00:00:00 2001 From: H4CK3R-01 Date: Wed, 6 Apr 2022 12:34:03 +0200 Subject: [PATCH] Prevent "out of memory" issue --- api/load_share_price.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/api/load_share_price.py b/api/load_share_price.py index c58f7a9..e0e9ea7 100644 --- a/api/load_share_price.py +++ b/api/load_share_price.py @@ -1,9 +1,11 @@ import datetime import os import threading +import time import requests import yfinance +from dotenv import load_dotenv def thread_function(s): @@ -20,6 +22,12 @@ def thread_function(s): requests.post(os.getenv("API_URL") + '/symbol', json=payload, headers={'Authorization': 'Bearer ' + token}) +def split(a, n): + k, m = divmod(len(a), n) + return (a[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in range(n)) + + +load_dotenv() username = os.getenv('ADMIN_EMAIL') password = os.getenv('ADMIN_PASSWORD') @@ -27,7 +35,10 @@ token = requests.post(os.getenv("API_URL") + '/user/login', json={"email": usern response = requests.get(os.getenv("API_URL") + '/symbols', headers={'Authorization': 'Bearer ' + token}).json()['data'] -for symbol in response: - x = threading.Thread(target=thread_function, args=(symbol,)) - x.start() +symbols = split(response, int(len(response) / 5)) +for symbol_list in symbols: + for symbol in symbol_list: + x = threading.Thread(target=thread_function, args=(symbol,)) + x.start() + time.sleep(10)