Prevent "out of memory" issue

This commit is contained in:
Administrator 2022-04-06 12:34:03 +02:00
parent d3a09ea940
commit cb7cc14529

View File

@ -1,9 +1,11 @@
import datetime import datetime
import os import os
import threading import threading
import time
import requests import requests
import yfinance import yfinance
from dotenv import load_dotenv
def thread_function(s): 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}) 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') username = os.getenv('ADMIN_EMAIL')
password = os.getenv('ADMIN_PASSWORD') 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'] response = requests.get(os.getenv("API_URL") + '/symbols', headers={'Authorization': 'Bearer ' + token}).json()['data']
for symbol in response: symbols = split(response, int(len(response) / 5))
x = threading.Thread(target=thread_function, args=(symbol,)) for symbol_list in symbols:
x.start() for symbol in symbol_list:
x = threading.Thread(target=thread_function, args=(symbol,))
x.start()
time.sleep(10)