How to use thread in Django - python

I want to check users' subscribed dates for certain period. And send mail to users whose subscription is finishing (ex. reminds two days).
I think the best way is using thread and timer to check dates. But I have no idea how to call this function. I don't want to make a separate program or shell. I want to combine this procedure to my django code. I tried to call this function in my settings.py file. But it seems it is not a good idea. It calls the function and creates thread every time I imported settings.

That's case for manage.py command called periodically from cron. Oficial doc about creating those commands. Here bit more helpful.
If you want something simpler then django-command-extensions has commands for managing django jobs.

if you need more then only this one asynchronous job have a look at celery.

using Django-cron is much easier and simple
EDIT: Added a tip
from django_cron import cronScheduler, Job
class sendMail(Job):
# period run every 300 seconds (5 minutes)
run_every = 300
def job(self):
# This will be executed every 5 minutes
datatuple = check_subscription_finishing()
send_mass_mail(datatuple)
//and just register it
cronScheduler.register(sendMail)

Related

Django Schedule/run tasks dynamically at particular time periodically from user's order in app (without celery)

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!
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

Django real time jobs

How Can I create real time actions in python / django?
for more info:
Some user add some thing to database also other user add same one too (not same as they are but they have similar property) at same time (and all times) program should being check if they are objects with similar property {if they are not same check both of them in other time with all other objects that may be added/edited on database}
these actions should be in real time or at last by few minuts appart.
for example:
for every(2min):
do_job()
or
while True:
do_job()
if i use second one program will stop.
You need to run a async task to check the objects in background. You can check this link for reference Celery doc
In case if you have any limitations using celery or a similar approach, the other way is to create a scripts.py inside your app(same level as models.py & views.py) and write logic and schedule this in cron or any scheduler based on your host server.

How to create scheduler in httpTrigger in azure function using python?

i have one httpTrigger where i have implemented cache we have a requirement where we have to update cache after 2 hr.
Solution 1:
we can expire the cache after 2 hour.. but we don't want to use this solution
Solution 2:
we want a function to get triggered (update_cache()) after every 2 hour.
I find out some library
But i am unable to get how i can implement this..
# i want to trigger this function every 2 hour
def trigger_scheduler_update():
logging.info("Hi i am scheduler and got triggered...")
schedule.every(2).hours.do(trigger_scheduler_update)
But the problem i am facing here is we have to write this kind of code.
# ref: https://www.geeksforgeeks.org/python-schedule-library/
while True:
# Checks whether a scheduled task
# is pending to run or not
schedule.run_pending()
time.sleep(1)
As its an infinite loop i can place it in http trigger is there a way i can implement a scheduler that run after 2 hr.
i don't know that can it be done using threading?
i found one more library but looks like it also won't work.
Your function is shut down after a period of time, unless you are on a premium plan. Even then you cannot guarantee your function keeps on running.
What cache are you referring to?
Note that you cannot do threading in azure functions and you shouldn't actually. Abandon the idea of refreshing the cache from your httpTrigger function and create a separate scheduleTriggered function to update the cache that your http function is using.

Need pythonic way to schedule one time jobs with arguments

The premise is that I have a script which checks a resource every morning, and retrieves times and URI of events, which will vary from day to day. I want to pass the time and URI location to a scheduler, so that a script designed to capture the event gets called at the event time, passing the location as a variable to the capture script.
At first glance crontab seems like the easiest way to do it, but every job is unique and will only run once, so it creates a lot of maintenance.
I don't have a suggestion for Python specifically, but given that you mentioned crontab as something you were considering, the "one-off" version of a crontab would be at.
'at' tutorial
'at' man page

Python daemonize

I would like to daemonize a python process, and now want to ask if it is good practice to have a daemon running, like a parent process and call another class which opens 10-30 threads.
I'm planning on writing a monitoring script for group of servers and would like to check every server every 5 mins, that each server is checked exactly 5minutes.
I would like to have it this way ( sort of speak, ps auxf style output ):
|monitor-daemon.py
\-check-server.py
\-check-server.py
....
Thank you!
Maybe you should use http://pypi.python.org/pypi/python-daemon
You can use supervisord for this. You can configure tasks to respond to events. The events can be manually created or automatically by monitoring processes or based on regular intervals.
It is fully customizable and written in Python.
Example:
[program:your_daemon_name]
command=your_daemon_process
# Add extra options here according to the manual...
[eventlistener:your_monitor_name]
command=your_monitor_process
events=PROCESS_STATE_RUNNING # Will be triggered after a program changes from starting to running
# Add extra options here according to the manual...
Or if you want the eventlistener to respond to the process output use the event PROCESS_COMMUNICATION_STDOUT or TICK_60 for a check every minute. The logs can be redirected to files and such so you can always view the state.
There's really not much to creating your own daemonize function: The source for Advanced Programming in the Unix Environment (2nd edition) is freely available: http://www.apuebook.com/src.tar.gz -- you're looking for the apue.2e/daemons/init.c file.
There is a small helper program that does all the work of creating a proper daemon, it can be used to wrap arbitrary programs; this might save some hassle.

Categories