From 261672a1f707d81863b05e4b87d03f98ec087792 Mon Sep 17 00:00:00 2001 From: ChristopherGSF Date: Mon, 17 Jul 2023 12:12:58 +0100 Subject: [PATCH] map reduce type task --- project/worker.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/project/worker.py b/project/worker.py index 32d6438..68a9df9 100644 --- a/project/worker.py +++ b/project/worker.py @@ -22,8 +22,9 @@ def startup_task(id_): return True -async def call_chatgpt_or_whatever(): +async def call_chatgpt_or_whatever() -> int: await asyncio.sleep(10) + return random.randint(5, 20) # Converts a Celery tasks to an async function def task_to_async(task): @@ -42,9 +43,9 @@ async def wrapper(*args, **kwargs): def async_parallel_task(id_): logger.info('-------> parallel task started %s' % id_) # Deliberately variable to mimic 3rd party API uncertainty - call_chatgpt_or_whatever() + result = call_chatgpt_or_whatever() logger.info('-------> parallel task complete %s' % id_) - return True + return result @celery.task() @@ -52,13 +53,13 @@ def sync_parallel_task(id_): logger.info('-------> parallel task started %s' % id_) sleep(10) logger.info('-------> parallel task complete %s' % id_) - return True + return random.randint(5, 20) @celery.task() -def reducer_task(id_): +def reducer_task(id_, ints_to_average: list[int]): logger.info('-----> reducer task started') # Deliberately short - idea being that when parallel task is paused it switches to this. sleep(0.3) logger.info('-----> reducer task complete') - return True \ No newline at end of file + return sum(ints_to_average) \ No newline at end of file