Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs/cookbooks django #197

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/cookbook/django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,69 @@ if that's something you would want. Let's add this view in our ``views.py`` file
status=HTTP_200_OK,
)

Actiavate Rocketry At Server Start
----------------------------------

Another way to integrate rocketry with django is to create a file in each app
called ``scheduled_tasks.py`` with the tasks referent to that app:

.. code-block:: python

from asgiref.sync import sync_to_async
from django.contrib.auth.models import User


def do_things_with_user():
print(f'X {User.objects.first()}!')


async def run_do_things_with_user_app_x():
await sync_to_async(do_things_with_user)()


def register_scheduled_tasks_app_x(app):
app.task('every 10 seconds', func=run_do_things_with_user_app_x)

And than, in the same module where the file ``wsgi.py`` is located, add a file called
``init_scheduled_tasks.py`` (or anywhere you would like, just make sure to import the file
correctly), which will import the register functions of each file of tasks and
initialize rocketry in another process:

.. code-block:: python

from multiprocessing import Process
from rocketry import Rocketry
from x.scheduled_tasks import register_scheduled_tasks_app_x
from y.scheduled_tasks import register_scheduled_tasks_app_y


def init_scheduled_tasks():
app = Rocketry(execution="async")

register_scheduled_tasks_app_x(app)
register_scheduled_tasks_app_y(app)

p = Process(target=app.run)
p.start()

After that, go to the ``wsgi.py`` file, import the ``init_scheduled_tasks`` function
and call it after ``application = get_wsgi_application()``:

.. code-block:: python

from django.core.wsgi import get_wsgi_application

...

application = get_wsgi_application()

from .init_scheduled_tasks import init_scheduled_tasks
init_scheduled_tasks()

Notice that if you are using gunicorn with multiple workers, each worker will create one
process of rocketry which will be running all the tasks. This is aimed primarily towards
development where you need to restart the server all the time and the server is single
threaded.

.. note ::
You will only need to use ``sync_to_async`` if you use the asynchronous ORM. The usage is well documented in
Expand Down