Hi.
I implement sync API with DRF (hello_world). I used celery for my own tasks (sub_task). I want when called hello_world API view, called sub_task in the background. I don’t want to wait for a broker connection or anything. I used the apply_async function but if sub_task can’t connect to the broker or anything (such as network time out, …) occurred exceptions.
I want to call sub_task in another thread. it’s a good idea?
import threading
from celery import shared_task
from rest_framework.response import Response
from rest_framework.views import APIView
@shared_task
def sub_task(self):
print("Hello Sub task")
class HelloWorldView(APIView):
def get(self, request, *args, **kwargs):
# ................................
# sub_task is a celery task
sub_thread = threading.Thread(target=sub_task.apply_async)
sub_thread.start()
# ................................
return Response(data={})