# Start another process with Django

**URL:** <https://forum.djangoproject.com/t/start-another-process-with-django/22176>\
**Category:** Deployment\
**Created:** [July 9, 2023, 2:30pm UTC](https://forum.djangoproject.com/t/start-another-process-with-django/22176 "2023-07-09T14:30:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheRealBecks](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/therealbecks/32/12616_2.png) [@TheRealBecks](https://forum.djangoproject.com/u/TheRealBecks)\
**Post date:** [July 9, 2023, 2:30pm UTC](https://forum.djangoproject.com/t/start-another-process-with-django/22176/1 "2023-07-09T14:30:38Z")

</div>

I’m writing a Django stock trading app and I’m at the point at creating the `Strategy` model. I want to be able to configure a strategy via the web gui (\<-- details regarding the model don’t matter) and then start the strategy with a click on `Start`. Now Django needs to start a new Python process (process, not thread, because I need a dedicated CPU core per strategy), but how can Django do that? How to start a process and let it run in (kind of) real-time in the background? There will be a data-stream from a stock broker and therefore the strategy needs to run a loop for each new value received via the data-stream. I also want to store the data in the database. The strategy also needs to communicate with the other Django models, so read and write with the Django ORM needs to be possible.

Do you know any tools, videos or articles that will point me into the right direction? 🙂 Tools that I already heard of:

- `django-q2`: [GitHub - django-q2/django-q2: A multiprocessing distributed task queue for Django based on Django-Q](https://github.com/django-q2/django-q2)
- `celery` with `django-celery-beat`: [GitHub - celery/celery: Distributed Task Queue (development branch)](https://github.com/celery/celery), [GitHub - celery/django-celery-beat: Celery Periodic Tasks backed by the Django ORM](https://github.com/celery/django-celery-beat)

…but as far as I understood these tools are needed for non-time-critical tasks as these are like a scheduler like e.g. the Linux `cron`.

Do you have any other ideas I can follow up? 🙂

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 9, 2023, 7:31pm UTC](https://forum.djangoproject.com/t/start-another-process-with-django/22176/2 "2023-07-09T19:31:00Z")

</div>

You don’t _want_ “Django” to do or manage that.

You want to use something like Celery, or even a worker task in channels. Both options do give you access to the database using the ORM.

You want to consider the complete life-cycle of the process - once it’s started, how long is it going to run? What is going to monitor the status of that process - possibly restarting it if it should fail for some reason? How many instances of these are going to be running? Do you have a way of limiting that number of instances?

This type of “process management” is one of those things that is an extremely bad fit for your Django wsgi process. Do it external to Django.

> [@TheRealBecks](#):
>
> …but as far as I understood these tools are needed for non-time-critical tasks as these are like a scheduler like e.g. the Linux `cron`.

That would be a mistaken conclusion. These tools are generally used to run tasks that will exist beyond the lifespan of the view that initiated them. It makes no assumptions regarding the timing of the tasks themselves.

---

<div class="post-metadata">

**Author:** ![TheRealBecks](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/therealbecks/32/12616_2.png) [@TheRealBecks](https://forum.djangoproject.com/u/TheRealBecks)\
**Post date:** [July 10, 2023, 7:34pm UTC](https://forum.djangoproject.com/t/start-another-process-with-django/22176/3 "2023-07-10T19:34:42Z")

</div>

Thanks for the answer! Looks like I missed the good functions at Celery, I will check it out 🙂

> You want to consider the complete life-cycle of the process - once it’s started, how long is it going to run? What is going to monitor the status of that process - possibly restarting it if it should fail for some reason? How many instances of these are going to be running? Do you have a way of limiting that number of instances?

That are exactly the questions I have, but still missing the answers… 😂
