【问题标题】:Run Django and GRPC same application运行 Django 和 GRPC 相同的应用程序
【发布时间】:2021-11-18 22:33:40
【问题描述】:

我正在开发 Django rest 框架项目,我还需要使用 gRPC。但我不知道如何同时运行 HTTP 服务器和 gRPC 服务器。就像在 .NET 中一样,它可以选择同时监听 HTTP1 和 HTTP2。

当我使用命令时

python manage.py runserver

然后 gRPC 不起作用

当我使用时

python manage.py grpcserver

然后 Rest API 不起作用

这个问题有什么解决办法吗?谢谢。

我使用了包:djangorestframeworkdjango-grpc

【问题讨论】:

    标签: python django grpc


    【解决方案1】:

    解决了

    我刚刚创建了一个新的自定义管理命令并同时运行它

    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)
    

    【讨论】:

    • 怎么用?
    猜你喜欢
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多