From 6b2a583b167f5fd5c65c5c27924c00fb9ce7d08e Mon Sep 17 00:00:00 2001 From: Queue A Date: Fri, 31 Jul 2026 15:47:19 +0200 Subject: [PATCH] Added explicit stdout print and direct task execution --- .../management/commands/run_asyncron_task.py | 48 +++++++++++++++++++ asyncron/models.py | 4 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 asyncron/management/commands/run_asyncron_task.py diff --git a/asyncron/management/commands/run_asyncron_task.py b/asyncron/management/commands/run_asyncron_task.py new file mode 100644 index 0000000..214638a --- /dev/null +++ b/asyncron/management/commands/run_asyncron_task.py @@ -0,0 +1,48 @@ +## +# +# Command: python manage.py run_asyncron_task +# +## + +import traceback, logging +import asyncio +import time + +from django.core.management.base import BaseCommand, CommandError +from django.conf import settings + +from asyncron.models import Task + +class bcolors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + +class Command(BaseCommand): + help = 'Start an Asyncorn Worker' + + def handle( self, *args, task_name, **kwargs ): + + task = Task.objects.filter( name__endswith = task_name ).first() + if not task: + print("Could not find this task, these are the options:\n", "\n - ".join( Task.registered_tasks )) + return + + import asyncio + print(f"Running Task: {task}") + trace = task.new_trace() + trace.show_prints = True + asyncio.run( trace.start() ) + trace.save() + + def add_arguments( self, parser ): + parser.add_argument('task_name', default = "") + + +# diff --git a/asyncron/models.py b/asyncron/models.py index 5c8e353..157b76f 100644 --- a/asyncron/models.py +++ b/asyncron/models.py @@ -239,5 +239,7 @@ class Trace( BaseModel ): def print( self, *args, sep = " ", end = "\n", file = None, flush = True ): #We get 'file' here to fool tasks that aren't self_aware assert hasattr(self, 'commit_on_new_print_task'), "trace.print needs to be called while a trace.commit_on_new_print task is active!" - self.stdout += sep.join( str(i) for i in args ) + end + string = sep.join( str(i) for i in args ) + end + self.stdout += string if flush: self.new_print.set() + if getattr(self, "show_prints", False): print( string )