CodexBloom - Programming Q&A Platform

working with Infinite Loop guide with Celery 5.x and Redis in Python 3.9

👀 Views: 73 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-28
celery redis python-3.x Python

I tried several approaches but none seem to work. Quick question that's been bugging me - I'm working on a project and hit a roadblock... I'm currently working with an infinite loop scenario with my Celery tasks when using Redis as a broker in my Python 3.9 application. The task seems to be retried indefinitely, despite the fact that I've set a maximum retry limit. Here's the relevant part of my code: ```python from celery import Celery, current_task import time app = Celery('tasks', broker='redis://localhost:6379/0') @app.task(bind=True, max_retries=3, default_retry_delay=10) def my_task(self): try: # Simulating a task that raises an exception raise ValueError('An behavior occurred!') except Exception as e: print(f'Task failed: {str(e)}') raise self.retry(exc=e) ``` I expected the task to stop after three attempts, but it keeps retrying indefinitely. I've already checked the Celery logs, and I see this message repeating: `[behavior/MainProcess] Task tasks.my_task[task_id] retrying in 10.00 seconds (3 retries left)`. I have also confirmed that the Redis server is running and accessible. I am using Celery 5.2.0 and Redis 6.0.8. To troubleshoot, I tried adding a print statement to monitor the retry attempts, but that didn't help clarify why it keeps looping. Any insights into what might be causing this behavior or how to properly handle failures in this scenario would be greatly appreciated! For context: I'm using Python on Windows. What's the best practice here? What's the best practice here? Has anyone else encountered this? I'm developing on Debian with Python. I'm open to any suggestions.