pythondjangogrpc

django, gunicorn, grpc: "Failed to pick subchannel" if run django grpc client with gunicorn


I am a beginner in the Django framework. I wrote an application consist of 2 microservices. One as a Django Rest Server and the other as a Django GRPC Server. the rest request translated to GRPC requests in the first server and sent to the second for the process.

      ------>|-----------|------->|-----------|
             |rest server|        |grpc server|
             |           |        |           |
      <------|-----------|<-------|-----------|

when I ran the first server with python manage.py runserver ip:port the application works properly. but when I ran the first server with gunicorn myapp.wsgi --workers=1 --bind=ip:port, a grpc request from the rest server randomly counter an error:

{"created":"@1593330482.005402565","description":"Failed to pick 
subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3981,"refer
enced_errors":[{"created":"@1593330482.005396918","description":"failed to connect to all 
addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_l
ine":394,"grpc_status":14}]}

the interesting thing is the grpc request was received by grpc server and it sent back the response.

wsgi.py:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')

application = get_wsgi_application()

Solution

  • I think I've found the problem. In view.py, for each request, I create a grpc channel. A channel takes time to connect to the server. I think if I send grpc request while the channel is not connected to the server, this error will happen. The code is under development. I will change the view.py to reuse the grpc channel. After that, if the error persists, I will use your suggestion and I will inform you about the result. thanks.