diff --git a/posthog/utils.py b/posthog/utils.py index d9a9cda7d3439..ceec0087d9238 100644 --- a/posthog/utils.py +++ b/posthog/utils.py @@ -10,6 +10,7 @@ import secrets import string import subprocess +import threading import time import uuid import zlib @@ -1192,10 +1193,22 @@ def wait_for_parallel_celery_group(task: Any, max_timeout: Optional[datetime.tim start_time = timezone.now() - while not task.ready(): - if timezone.now() - start_time > max_timeout: - raise TimeoutError("Timed out waiting for celery task to finish") - time.sleep(0.1) + event = threading.Event() + + while not event.is_set(): + if task.ready(): + event.set() + else: + if timezone.now() - start_time > max_timeout: + logger.error( + "Timed out waiting for celery task to finish", + methods=dir(task), + task=task, + timeout=max_timeout, + start_time=start_time, + ) + raise TimeoutError("Timed out waiting for celery task to finish") + event.wait(0.1) return task