django-background-task | A database-backed work queue for Django
kandi X-RAY | django-background-task Summary
kandi X-RAY | django-background-task Summary
A database-backed work queue for Django
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of django-background-task
django-background-task Key Features
django-background-task Examples and Code Snippets
def handle_uploaded_file(f):
with open('some/file/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
from background_task.models import Task
if not Task.objects.filter(verbose_name="my_task_name").exists():
tasks.my_task(verbose_name="my_task_name")
Community Discussions
Trending Discussions on django-background-task
QUESTION
django-background-tasks package does not work when django-hosts package is configured.
My background task function looks something like this:
...ANSWER
Answered 2021-Apr-26 at 14:32Did you set the DEFAULT_HOST
setting? Since there's no HTTP request in the CLI/background task, there's no other way to detect the HOST, which could be the cause of your errors.
Also, posting an actual error message with your question would be useful.
Set the DEFAULT_HOST setting to the name of the host pattern you want to refer to as the default pattern. It'll be used if no other pattern matches or you don't give a name to the host_url template tag.
QUESTION
I have a simple django app that provides a login page and an authenticated dashboard. I have created a data model, and the dashboard view displays this data, taken from the django database.
I have created a class that processes the data in this model. The class is not involved in the app views directly, it only modifies db content. A repeating function registered with django-background-tasks is used to call various class functions to process db data.
Currently I define the class and the django-background-task function in a single file in the django app. I instantiate the class in this file too.
In order to schedule the repeating django-background-task function I need to call it once from somewhere within the django project. Where in the django file structure would be best to do this?
In apps.py? Is the db ready at this point?
Thanks.
...ANSWER
Answered 2021-Mar-15 at 12:32I found a suggested answer to this question in the following post:
https://medium.com/@robinttt333/running-background-tasks-in-django-f4c1d3f6f06e
The author suggests placing the function to be called repeatedly in a file called tasks.py - I missed this point in the readme docs of the package. Also the author suggests adding the call that triggers the initial scheduling of the repeated task in urls.py.
I tried this solution and it worked fine, although using urls.py doesn't seem the most inuitive or logical location. If anyone has any other suggestions I would be interested to hear them.
Ta.
QUESTION
So, I am currently working on a django project hosted at pythonanywhere, which includes a feature for notifications, while also receiving data externally from sensors through AWS. I have been thinking of the best practice in order to implement this.
I currently have a simple implementation which is a view that checks all notifications and does the actions as needed if required, with an always-on task (which simply means a script that is running independently) sending a REST request to the server every minute.
Server side:
views.py:
...ANSWER
Answered 2021-Feb-25 at 10:55To use Django background tasks on PythonAnywhere you need to run it using an always-on task, so it is not an alternative, but just the other use of always-on tasks.
You can also access your Django code in your always-on task directly with some kind of long-running management command, so you do not need to hit your web app with a special request.
QUESTION
On django-background-tasks==1.1.11
(Django==2.2
, Python 3.6.9
), I have this problem where everytime I run python manage.py migrate
, the table background_task_completedtask
gets deleted. This breaks my background tasks. So far I have found a way to reverse it, as it is a separate migration from the initial one, meaning I can just python manage.py migrate background_task 0001_initial
to restore it, but this does mean it will still be removed next migration.
Any ideas for a more permanent solution?
ANSWER
Answered 2021-Jan-28 at 15:26Found a (somewhat hacky) permanent solution myself:
By faking migrations (python manage.py migrate --fake
(or python manage.py migrate appname --fake
)), you make django think the migration has been executed without actually executing it. By doing this with the migration that was bothering me, I managed to get everything working again.
QUESTION
I realize similar questions have been asked however they have all been about a sepficic problem whereas I don't even now how I would go about doing what I need to.
That is: From my Django webapp I need to scrape a website periodically while my webapp runs on a server. The first options that I found were "django-background-tasks" (which doesn't seem to work the way I want it to) and 'celery-beat' which recommends getting another server if i understood correctly.
I figured just running a seperate thread would work but I can't seem to make that work without it interrupting the server and vice-versa and it's not the "correct" way of doing it.
Is there a way to run a task periodically without the need for a seperate server and a request to be made to an app in Django?
...ANSWER
Answered 2021-Jan-04 at 18:46'celery-beat' which recommends getting another server if i understood correctly.
You can host celery (and any other needed components) on the same server as your Django app. They would be separate processes entirely.
It's not an uncommon setup to have a Django app + celery worker(s) + message queue all bundled into the same server deployment. Deploying on separate servers may be ideal, just as it would be ideal to distribute your Django app across many servers, but is by no means necessary.
QUESTION
I am trying to run a some machine learning computation task in the background due to heroku's 30 seconds timeout. I was trying to implement django-background-tasks. here's my code:
...ANSWER
Answered 2020-Dec-22 at 14:59Background tasks are usually asynchronous, meaning the process will run on the background. Expecting an instant result from your request does not make any sense in my opinion. It would make more sense to receive eg. 201 created, notifying the user that the request has been received and will be processed.
When the application is processing it, it could write the result to the database and expose the result over an API or interface.
QUESTION
I am trying to collect application-specific Prometheus metrics in Django for functions that are called by django-background-tasks.
In my application models.py
file, I am first adding a custom metric with:
ANSWER
Answered 2020-Sep-29 at 13:01I ended up creating a decorator leveraging the Prometheus Pushgateway feature
QUESTION
I have a Django web application and I'm trying to make an ajax call for uploading a large image. If I can upload the image I am trying to upload, I'll read the image with the pythonocc library and return the volume information. Since this process takes a long time, I am trying to do it using the django-background-tasks library. According to what I'm talking about, the ajax request that I am trying to take the image and send it to the view is as follows.
...ANSWER
Answered 2020-Aug-24 at 07:03A. You should restrict your view method to the actual method you are expecting using the require_http_methods decorator
QUESTION
I have a webapp that monitors sites that users add for any changes. To do this, I need to have some sort of separate background thread/process that is constantly iterating through the list of sites, pinging them one at a time, and emailing any users that are monitoring a site that changes. I am currently using a thread that I initialize at the end of my urls.py file. This works fine with Django's development server, but it begins to break down once I deploy it to Heroku with Gunicorn. As soon as there are multiple connections, multiple copies of the worker thread get started, as Gunicorn starts more worker threads to handle the concurrent connections (at least, this is what I think is the reason behind the extra threads is). This causes duplicate emails to be sent out, one from each thread.
I am now trying to find another means of spawning this worker thread/process. I saw a similar inquiry here, but when I tried the posted solution, I was unable to reference the models from my Django app and received this error message when I tried to do so:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I have also tried using django-background-tasks
, which is frequently recommended as a simple solution for issues like this. However, it doesn't seem suited for looping, continuous processes. The same goes for Celery and other solutions like it. I am just looking for a way to start a separate worker Dyno that continuously runs in the background, without a queue or anything like that, and is able to use the models from my Django app to create QuerySets that can be iterated through. What would be the best way to do something like this? Please let me know if any more information would help.
ANSWER
Answered 2020-Jul-11 at 06:40You could try editing the code so that the parts that handle the email specifically aren't tried so intrinsically to the django model, such that both the django model and this secondary application interact with the standard python class/module/object/etc, instead of trying to graft out the part of django you need elsewhere.
Alternatively, you can try using something like threading.Lock if your app is actually using threads inside one interpreter to prevent multiple messages from sending. There is also a multiprocessing.Lock that may work if the threading one does not.
Another option would be to make it so each requested change would have a unique value to it, preferably something based on the contents of the changes themselves. IE if you have something like:
QUESTION
I am new to Django and the django-background-tasks package.
I am facing an issue that I couldn't do/start background task unless I forcefully run the command process_tasks , that is python manage.py process_tasks. I want to do/start background task without run the process_tasks command.
i am creating rest api .So i want to run the background task automatically, with out writing the command. how it is possible. ? i found a code. thats is shown in below.. But i didt get in which file i put the code.
...ANSWER
Answered 2020-Jul-06 at 13:26Firstly, I would like to say that I prefer to consume the tasks with the command python manage.py process_tasks
running for a few reasons, of which two are worth mentioning:
- It is tied to django command, thus if your app has problem, it makes sense to stop process_tasks too.
- Following the first reason, you might want to keep the process_tasks running as long as your app is working fine. Thus, should not write another script to invoke the process_tasks.
With these being said, if you really want to avoid manually opening a terminal and call the command line, I recommend you to implemented a function using subprocess
to invoke the python manage.py process_tasks
before you call the background tasks.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install django-background-task
No Installation instructions are available at this moment for django-background-task.Refer to component home page for details.
Support
If you have any questions vist the community on GitHub, Stack Overflow.
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page