Started threadpoolexecutor branch

This commit is contained in:
Joan
2023-08-23 12:54:13 +02:00
parent 79004c76fa
commit 01dc0f2695
2 changed files with 35 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import threading
import time
import concurrent.futures
import worker
import walladb
import helpers
import constants
def work():
searches = []
products = walladb.get_all_valid_products()
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
future_to_product = {executor.submit(worker.Worker.first_run, product, 60): product for product in products}
for future in concurrent.futures.as_completed(future_to_product):
product = future_to_product[future]
try:
searches[product['id']] = future.result()
#first_run_list = future.result()
except Exception as e:
print(f"Error occurred: {e}")
else:
print(f"First run for {product['name']} - {product['id']} got: {searches[product['id']]}")

View File

@@ -279,6 +279,17 @@ def get_all_products():
con.close() con.close()
return ret return ret
def get_all_valid_products():
with connect_db() as con:
#con.row_factory = dict_factory
with con.cursor(dictionary=True, prepared=True) as cur:
cur.execute(f"SELECT products.* FROM products WHERE products.telegram_user_id IN (SELECT users.telegram_user_id FROM users WHERE active = 1)")
try:
ret = cur.fetchall()
except:
ret = None
return ret
def add_product(product): def add_product(product):
condition = 'all' condition = 'all'
product_name = product.get('product_name').lower() product_name = product.get('product_name').lower()