1

I am creating a Django App where the user can schedule some tasks to happen at a particular time.

for example. in google calendar we tell google what we will be doing tomorrow and then at right time it sends us a notification.
similarly here I want it to be flexible, for instance, the user can tell Django the time and function to run. Django will wait for the time and then run the function.

like he said turn off lights at 12 pm then Django will do it.
or for example:-
user says remind me to go to the gym in 30 minutes
And then after 30 minutes, he gets a notification.

Actually the tasks are added dynamically so we can't hardcode them at first.

code:-

skills.py # all the functions defined by user are imported in it
# for example take this task
def turn_off_light(room, id, *args, **kwargs):
    # turned off light using python (your logic)
    print(f"light no {id} is turned off!")

there's a Django model known as Function in which this function is stored and the user can access it easily.

I want users to select this function within the application and also give parameters and time and Django will run it at that time!

In short what I need is that the user from within the application is able to set the time of a function(or task) to run at a particular time(maybe periodically or once or maybe in the situation) and Django runs it on that particular time.

Note: user will also give args and kwargs(I am taking care of that). All I need is Django run the function with those args and kwargs.

(only Django method without celery or something will be appreciated)

Thanks!

r4mbhardwaj
  • 101
  • 1
  • 9
  • Here is an example of scheduling in Django with apscheduler library: https://medium.com/@kevin.michael.horan/scheduling-tasks-in-django-with-the-advanced-python-scheduler-663f17e868e6 Alternately you could use the scheduling system on the server (crontab for linux) as well. – It works on my pc Dec 14 '21 at 11:52
  • Actually i tried it but i am stuck. I want it to schedule tasks and run them. Those tasks are dynamic functions, so they are not written at first... – r4mbhardwaj Dec 14 '21 at 11:56

1 Answers1

0

Without Celery and using Django the best way to do is to create custom django-admin commands with Cron

For example :

  • Create customer command called calendar_routine.py
  • Create a cron schedule to call your function from your server at a given time

Otherwise there is no way to do it in pure Python/Django

Gaëtan GR
  • 1,380
  • 1
  • 5
  • 21
  • Actually there is no routine, tasks are added dynamically, by user. For example turn off lights after 30 minutes – r4mbhardwaj Dec 14 '21 at 11:59
  • Yes but my example is very light since you did not provide any code. The user can create task with a specifc time and it is up to you to create function in which you can filter through the tasks to be execute at the current time. The most challenging thing to do here is to create a custom function – Gaëtan GR Dec 14 '21 at 12:02
  • Now I have Edited to question and given example code, that's why your answer won't work... I am working with user info in the function and its all parameters also – r4mbhardwaj Dec 14 '21 at 12:17
  • SO is to help with specific question, your function in your question is just a print statement, the logic is up to you to implement, but the mechanism is here. – Gaëtan GR Dec 14 '21 at 13:02
  • yes, but its not just print statement, that print statement was an example, there can be anything. like telling weather in morning everyday or like make me a coffee when I come from work. so that can do anything. all I need is to run function when user says he wants it to run at that particular time and that from within application, dynamically not hardcoded, user will tell when and which functions and how to run it with its args and kwargs – r4mbhardwaj Dec 14 '21 at 14:11