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.
Related
currently I have started working with Locust. I follow the class-picker docs and practice with a simple test, but quickly realized that every time I increase the number of users during the test, Locust will reset the statistics table. Besides, Locust behevior of ramping up users is quite strange: instead of increasing from 2 users to 5 users, it set the number of users to 0 first and then increase to 5. Is that an obvious thing when running Locust in class-picker mode?
Here is test.py
from locust import HttpUser, constant, task
class MyReqRes1(HttpUser):
wait_time = constant(1)
host = "http://example.com"
#task
def get_users(self):
res = self.client.get("/hello")
print(res.status_code)
class MyReqRes2(HttpUser):
wait_time = constant(1)
host = "http://example.com"
#task
def get_users(self):
res = self.client.get("/world")
print(res.status_code)
And here is my command to run:
locust -f test.py --class-picker
I am trying to keep Locust ramping up users normally (the way it do without --class-picker arguments) and keep the statistic table as well.
The user class UI picker is designed to let you choose a user class to use for the test run. This means that user class will be used for the whole duration of the test. If you want to choose a different user class, you need to start a new test which results in the behavior you described: Locust stops all currently running users, resets the stats, switches user classes, starts the new test by spawning new users at your defined spawn rate to reach the number of desired users.
In other words, it's designed to let you have multiple different test scenarios defined in the same file and let you choose the one you want at run time.
The user class UI picker does not allow you to choose one user class, start a test and get X number of users, choose another class, add Y users, choose another class, add Z users, end up with X+Y+Z running users, which sounds like what you're trying to do. There is not currently a way to accomplish that.
Of course, you're welcome to put together a pull request with that kind of behavior and it can be reviewed and perhaps included in a future version.
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
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
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.
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)