Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Django and GRPC same application

I'm working on the Django rest framework project and I need to work with gRPC as well. But I don't know how to manage to run both HTTP server and gRPC server same time. Like in .NET it has option to listen both HTTP1 and HTTP2.

When I use command

python manage.py runserver

then gRPC doesn't work

and when I used

python manage.py grpcserver

then Rest API doesn't work

Is there any solution to this problem? Thanks.

I used packages: djangorestframework and django-grpc

like image 315
Phat Huynh Avatar asked Sep 18 '25 16:09

Phat Huynh


1 Answers

Solved it

I just created a new custom management command and run both of it

from django.core.management.base import BaseCommand
from subprocess import Popen
from sys import stdout, stdin, stderr
import time
import os
import signal


class Command(BaseCommand):
    help = 'Run all commands'
    commands = [
        'python manage.py grpcserver',
        'python manage.py runserver'
    ]

    def handle(self, *args, **options):
        proc_list = []

        for command in self.commands:
            print("$ " + command)
            proc = Popen(command, shell=True, stdin=stdin,
                         stdout=stdout, stderr=stderr)
            proc_list.append(proc)

        try:
            while True:
                time.sleep(10)
        except KeyboardInterrupt:
            for proc in proc_list:
                os.kill(proc.pid, signal.SIGKILL)
like image 128
Phat Huynh Avatar answered Sep 21 '25 05:09

Phat Huynh