From 01dc0f2695b7cb9e880bbc26e3a7e270a3a5df33 Mon Sep 17 00:00:00 2001 From: Joan Date: Wed, 23 Aug 2023 12:54:13 +0200 Subject: [PATCH] Started threadpoolexecutor branch --- wallamanta/search_manager.py | 24 ++++++++++++++++++++++++ wallamanta/walladb.py | 11 +++++++++++ 2 files changed, 35 insertions(+) create mode 100644 wallamanta/search_manager.py diff --git a/wallamanta/search_manager.py b/wallamanta/search_manager.py new file mode 100644 index 0000000..51d6376 --- /dev/null +++ b/wallamanta/search_manager.py @@ -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']]}") \ No newline at end of file diff --git a/wallamanta/walladb.py b/wallamanta/walladb.py index 781a069..a58fd57 100644 --- a/wallamanta/walladb.py +++ b/wallamanta/walladb.py @@ -279,6 +279,17 @@ def get_all_products(): con.close() 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): condition = 'all' product_name = product.get('product_name').lower()