From bc71257782517836b59139bf8bdb4976c07f4ea0 Mon Sep 17 00:00:00 2001 From: Joan Cano Date: Wed, 16 Aug 2023 11:40:32 +0200 Subject: [PATCH] Enabled tracemalloc temporary --- wallamanta/test.py | 21 +++++++++++++++++++++ wallamanta/wallamanta.py | 17 +++++++++++++++++ wallamanta/worker.py | 16 +++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 wallamanta/test.py diff --git a/wallamanta/test.py b/wallamanta/test.py new file mode 100644 index 0000000..10a20fe --- /dev/null +++ b/wallamanta/test.py @@ -0,0 +1,21 @@ +import requests +import json + +TELEGRAM_TOKEN="6282777602:AAEQNT1t2TzcnMjfSQoYspOuVi2rb69SJl0" +test = "hola" + +keyboard = {'inline_keyboard': + [[{'text':'Ir al anuncio','url':f'https://es.wallapop.com/item/{test}'}]] + } +print(keyboard) +files = { + 'chat_id': (None, "10101691"), + 'photo': open(f"/Users/jcano/Desktop/test.png", 'rb'), + 'caption': (None, "texto"), + 'parse_mode': (None, 'MarkdownV2'), + 'reply_markup': (None, json.dumps(keyboard)), + } + +response = requests.post(f'https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendPhoto', files=files) + +print(response.content) \ No newline at end of file diff --git a/wallamanta/wallamanta.py b/wallamanta/wallamanta.py index e760f93..a2c9c8f 100644 --- a/wallamanta/wallamanta.py +++ b/wallamanta/wallamanta.py @@ -5,6 +5,7 @@ import walladb import constants import account_checker import time +import tracemalloc from worker import Worker from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ForceReply @@ -439,10 +440,26 @@ async def list_threads(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No await update.message.reply_markdown_v2(helpers.telegram_escape_characters(f"{threads_string}")) def count_threads(): + tracemalloc.start() time.sleep(10) while True: + snapshot1 = tracemalloc.take_snapshot() logging.info(f"=== There are: {threading.active_count()} threads. ===") time.sleep(60) + snapshot2 = tracemalloc.take_snapshot() + top_stats = snapshot2.compare_to(snapshot1, 'lineno') + + logging.info("[ Top 10 differences ]") + for stat in top_stats[:10]: + logging.info(stat) + + top_stats = snapshot2.statistics('lineno') + + logging.info("[ Top 10 ]") + for stat in top_stats[:10]: + logging.info(stat) + + def main()->None: global SEARCH_THREADS_LIST diff --git a/wallamanta/worker.py b/wallamanta/worker.py index ad88f1c..d9a3cac 100644 --- a/wallamanta/worker.py +++ b/wallamanta/worker.py @@ -4,7 +4,7 @@ import logging import helpers import walladb import constants -import asyncio +import tracemalloc # Enable logging logging.basicConfig( @@ -98,6 +98,7 @@ class Worker: helpers.random_wait() # Random wait to make requests separated in time in order to prevent API rate limit exec_times = [] while True: + snapshot1 = tracemalloc.take_snapshot() #logging.info(f"List for {product['product_name']} length is: {len(list)}") if not helpers.is_valid_request(product) or self._stop == True: logging.info(f"{product['product_name']} not valid anymore, exiting worker") @@ -138,6 +139,18 @@ class Worker: time.sleep(constants.SLEEP_TIME) exec_times.append(time.time() - start_time) logging.info(f"\'{product['product_name']}\' for {walladb.get_user(product['telegram_user_id'])} ({product['telegram_user_id']}) node-> last: {exec_times[-1]:.2f} max: {self.get_max_time(exec_times):.2f} avg: {self.get_average_time(exec_times):.2f}") + snapshot2 = tracemalloc.take_snapshot() + top_stats = snapshot2.compare_to(snapshot1, 'lineno') + + logging.info(f"[ Top 10 differences {product['product_name']} ]") + for stat in top_stats[:10]: + logging.info(stat) + + top_stats = snapshot2.statistics('lineno') + + logging.info(f"[ Top 10 {product['product_name']} ]") + for stat in top_stats[:10]: + logging.info(stat) def has_excluded_words(self, title, description, excluded_words): if len(excluded_words) > 0: @@ -174,6 +187,7 @@ class Worker: self._stop = True def run(product): + tracemalloc.start() worker = Worker() try: list = worker.first_run(product)