0

I use Django and Django Rest Framework for my internal API and I use Vue.js for my frontend. The backend (API) and the frontend are totally separated.

I need to run a background task (every time a user is created) and I am considering 2 solutions:

  1. Call (with a post_save signal) a function that runs the task.

Note that this function will call a 3rd party API. The call might fail for various reasons and/or run during a long period ~20sec.

  1. Create a background task

With Redis or RabbitMQ or django-background-tasks.

Which solution should I go for ? If both solutions are acceptable, what would be the limitations/advantages of each one ?

1 Answers1

0

You might need django celery. This is a great package for background tasks for django, you can choose either Redis or RabbitMQ as the broker, where the brokers doesn't matter much on my opinion.

Why can this be a good solution for your problem?

  1. This is easy to install where you just need to install the django celery and redis(I prefer redis), configure some settings and you have now async functions.
  2. You might need soon a scheduled task, where you just need to install its additional package.
  3. You only need to build type function and attach a decorator for it to be async.

from celery import shared_task @shared_task def add(x,y): return X+y

and call it anywhere in you code

add.delay()

you know how background task.

reon
  • 106
  • 1
  • Thanks. I understand that it works this way and I used to use celery in previous projects for scheduling tasks and I see the advantages. But this is a conceptual question and I'd like to know the pros/cons for each option for the case where front end and back are separated. @reon – Yassine Belmamoun Jul 20 '18 at 07:25
  • pros and cons depend on your exact needs now and in the future and the budget and and and... question is off topic – Adelin Jul 20 '18 at 07:33
  • @YassineBelmamoun it actually doesn't matter if you have separate back or front ends, at the end of the day your post goes to backend where you can just manipulate something maybe using signal or in custom views if you have one, call the method you wanted. Now your problem is what to choose for background task, you might want to browse [https://stackoverflow.com/questions/29539443/redis-vs-rabbitmq-as-a-data-broker-messaging-system-in-between-logstash-and-elas] [https://stackshare.io/stackups/rabbitmq-vs-redis] – reon Jul 20 '18 at 07:41
  • @reon, It does matter. If you don't separate the backend and frontend, when you do a request, you won't receive the answer until the request is completed. – Yassine Belmamoun Jul 24 '18 at 07:35
  • @Adelin I am asking about technical pros and cons for this specific situation. Is there any technical difference between using solution 1 and solution 2. – Yassine Belmamoun Jul 24 '18 at 07:38