I'm getting some data from an API every 30 seconds and insert to mongoDB periodically. hence, I used celery to process it in the background. celery.py as follow:
@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
from example.tasks import API
sender.add_periodic_task(30, API.s(), name='Api')
I have tasks.py as follow:
@shared_task
def API():
data=request.get(url).json()
db.collectionName.insert_many(data)
Then, I used two commands to run the task in the background periodically:
celery -A DjangoCelery1 beat -l info
celery -A DjangoCelery1 worker -l info
Everything all right and data is getting from the API and saved in MongoDB. Now, I want to able to start and stop all running tasks by press a button on a web page.
What should I do for this?
I found my answer on this page: https://realpython.com/asynchronous-tasks-with-django-and-celery/#reader-comments