The following code snippet is for a short custom management command. Stylistically, the name you choose for this file should reference the outcome of the task when it runs.Īt this stage, the app’s file tree will look similar to this: some_app/ĭjango automatically maps the name of the command file to the command that is run in the CLI ( python manage.py print_book_titles) Code the command’s logic Within commands create a file named something similar to print_book_titles.py.Within management create a commands directory.At the root level of the Django app, create a directory called management.The Python file that contains the code for the custom command is nested inside several directories: In this tutorial, you’ll be working within one of the apps and not the overall Django project. In our Django settings and have Celery use those values.A Django project contains one or more apps. The config_from_object is important so we can put CELERY_* settings It doesn't really matter what variable you assign the Celery() object to.Ĭelery will find it as long as it's at the top level of the module. We can import the # tasks files some other way if we prefer. tasks # files and registers any tasks it finds in them. config_from_object ( "nf:settings", namespace = "CELERY" ) # When we use the following in Django, it loads all the. Process that we haven't mentioned yet, but that's enough for now.įrom celery import Celery # Create default Celery app app = Celery () # namespace='CELERY' means all celery-related configuration keys # should be uppercased and have a `CELERY_` prefix in Django settings. We'll talk more later about the queue, the workers, and another important You can have many workers, possibly on many different servers, but we'll It'll pull off the first and execute it, then go back to wait for more. In order for the task to be executed, some other process, called a worker, Putting a task on a queue just adds it to a to-do list, so to speak. You can have many queues, but we'll assume When a task is ready to be run, Celery puts it on a Or you could have your function batchjob called every To call your function task1 with arguments (1, 3, 3) after five You can think of scheduling a task asĪ time-delayed call to the function. In the database, and then your web application has access to the You can write a task to do that work, thenĪsk Celery to run it every hour. ForĮxample, maybe every hour you want to look up the latest weather Your app has no control over how long the remote server will take to respond,Ĭase 2: Another common situation is wanting to run code regularly. This is common if you need to access a remote server to handle the request. You can use Celery to have your long-running codeĬalled later, and go ahead and respond immediately to the web request. You don't really need to run that code before responding to the web Why might this be useful? Here are a couple of common cases:Ĭase 1: Suppose a web request has come in from a user, who is waitingįor the request to complete so a new page can load in their browser.īased on their request, you have some code to run that's going to takeĪ while (longer than the person might want to wait for a web page), but The purpose of Celery is to allow you to run code according to a schedule.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |