Kubernetes容器内Docker容器的响应缓慢

时间:2019-04-12 05:02:14

标签: python docker grpc

我在Kubernetes容器内部署了一个由grpc2流双向应用程序组成的Docker容器。当Pod启动时,应用程序运行正常,但是来自应用程序的响应速度很慢。如果我在Pod内手动运行该应用程序,则响应非常好。

在Kubernetes容器内使用Docker启动应用程序是否有问题?

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import psutil
import os
#q = ""
#for i in cpul:
#    q+=str(i)+","
#q= q[:-1]    
#os.system("taskset -p -c %s %d" % (q, os.getpid()))
import argparse
import collections
import queue as Queue
import grpc
import webrtcvad
from proto import stt_pb2
from proto import stt_pb2_grpc
from pytz import timezone
import datetime
import os
import threading
import time
import uuid
import wave
import requests
from itertools import cycle
from concurrent import futures
import stream
import subprocess
import json
from logger import get_logger
import sys
import multiprocessing
logger = get_logger(__name__)

class IterableQueue():
        def __init__(self, Q):
                self.Q = Q
                self.predicate = True

        def __iter__(self):
                return self

        def _check(self, x):
                if x['is_final'] == True:
                        self.predicate = False

        def __next__(self):
                if self.predicate:
                        item = self.Q.get()
                        self._check(item)
                        return item
                else:
                        raise StopIteration

class Listener(stt_pb2_grpc.ListenerServicer):
    def __init__(self):
        self.frames = {}
        self.lang={}
        self.emailid = {}
        self.response_time = {}
        self.vendorname = {}
        self.timestamp = {}
        self.userid = {}
        logger.info("Server ON")

    def UpdateDB(self,key,frames,transcript,confidence,email="default@b.c"):
       // Updating data to database

    def _splitStream(self, request_iterator, listQueues, key):
        // logic to split stream

    def _mergeStream(self, asr_response_iterator, responseQueue, asr, key):
        for asr_response in asr_response_iterator:
           // merge stream code
            responseQueue.put(toClient_json)

    def DoAToB(self, request_iterator, context):
        print("Serving request using "+str(os.getpid()))
        logger.info("Serving request using "+str(os.getpid()))
        // logic to do decoding


def serve(port,i):
    server = grpc.server()
    ps = psutil.Process()
    os.system("taskset -p -c %d %d" % (i, os.getpid()))
    server.add_insecure_port('[::]:%d' % port)
    print("Server starting in port "+str(port)+" with cpu "+ str(i))
    server.start()
    try:
        while True:
            time.sleep(60 * 60 * 24)
    except KeyboardInterrupt:
        server.stop(0)


if __name__ == '__main__':
    ps = psutil.Process()
    cpul=ps.cpu_affinity() # CPU affinity list for the process

    no_cpu=os.environ["cpu_server"] # Number of CPUs from pod spec
    #num_cpus = len(cpul)

    first_port = 9000
    ports = []
    for i in range(int(no_cpu)):
        portnum = first_port + i
        ports.append(str(portnum))
    port_pool = cycle(ports)

    #print("No of Cpu's: "+no_cpu)

    if ( len(cpul) == int(no_cpu) ):
        # Exclusiveness in set. Bind to cpu list
        for i in cpul:
            p = multiprocessing.Process(target=serve,args=(int(next(port_pool)),i))
            p.start()
    else:
        # No exclusiveness. Bind to first "no_cpu" cpus.
        for i in range(0,int(no_cpu)):
            p = multiprocessing.Process(target=serve,args=(int(next(port_pool)),i))
            p.start()

我们正在手动启动grpc双向应用程序,以得到很好的响应。记录结果的详细信息:

实际:

last chuck of bidirectional stream time taken: 1.287374

预期:

last chuck of bidirectional stream time taken: 0.068374

0 个答案:

没有答案