Example #1
0
def start_server(server_address: str):
    """
    启动gRPC服务
    :param server_address: 服务地址ip:port
    :return:
    """

    # 创建一个线程用于执行定时任务
    scheduler = Scheduler([])
    scheduler.start()
    # gRPC 目前只支持concurrent.futures.ThreadPoolExecutor
    # 参考https://grpc.io/grpc/python/grpc.html#create-server
    thread_pool = futures.ThreadPoolExecutor(max_workers=10)
    grpc_server = grpc.server(thread_pool, maximum_concurrent_rpcs=10)
    quote_service_pb2_grpc.add_QuoteServiceServicer_to_server(
        QuoteService(), grpc_server)
    grpc_server.add_insecure_port(server_address)
    grpc_server.start()

    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    # 在进程关闭前关闭所有资源
    except (KeyboardInterrupt, SystemExit):
        # 必须要首先关闭进程池,用join方法阻塞主线程
        _POOL.close()
        _POOL.join()
        print("POOL is closed!")
        # 然后关闭grpc server,等待时间设为20秒,保证请求能正常返回
        grpc_server.stop(20)
        print("grpc server is closed!")
        # 保证所有后台资源都合理的关闭掉
        scheduler.terminate()
        print("Service exits.")
Example #2
0
class Handler(object):

    def __init__(self, request):
        self.request = request
        self.wsEngine = request.META.get('wsgi.websocket', None)
        self.wsRms = RmsWsClient(self)

        self.stop = False
        self.waitingQueue = MyQueue()
        self.runningQueue = MyQueue()


    def process(self):
        """
        TODO, Handle request 
        1] open to RMS websocket
        2] New waitingQueue and runningQueue and defines maximum size
        3] Launch comsumer thread, then monitor runningQueue
        4] Two thread do their work
            How to use Queue?
            MainThread
            1] new a waitingQueue, like a proceduer, everytime, main thread receives client request, then put it into waitintQueue
            2] Everytime, main thread receives client release, call RMS release resources, then remove resource from runningQueue,
                How to remove resources from runningQueue, need to talk with RMSs
    
            CosumerThread
            2] new a runningQuueu, like a consumer, its has maximum size, 
                when runningQueue reaches its maximun size, consumer thread sleep 1 second, then poll again
                when runningQueue resources -1, consumer thread get a new resource from waiting queue,
                    if this unit is BUSY by RMS, put it to the end of the waiting queue, then loop get next
                    if this unit is Y/N by RMS, put it to the runningQueue + 1, then return it to client
                    Need to talk RMS
        """

        self.wsRms.connect()

        self.scheduler = Scheduler(self)
        self.scheduler.setDaemon(True)
        self.scheduler.start()

        while not self.stop:
            json = self.wsEngine.receive()
            if json == None:
                time.sleep(1)
                continue
            print "------->Receive from lib: %s" %json
            message = Message().restore(json)

            if message.getCmd() == Message.CMD_REGISTER:
                self.waitingQueue.append(message)

            elif message.getCmd() == Message.CMD_RELEASE:
                self.wsRms.release(message.getRes())
                self.runningQueue.remove(message)

        self.scheduler.stop()

    def stop(self):
        self.stop = True
Example #3
0
 def start(self):
     cherrypy.config['tools.encode.on'] = True
     cherrypy.config['tools.encode.encoding'] = 'utf-8'
     cherrypy.config['tools.sessions.on'] = True
     cherrypy.config['server.environment'] = 'production'
     cherrypy.config['engine.autoreload_on'] = True
     cherrypy.config['engine.autoreload_frequency'] = 5
     
     configfile = get_absolute_path('web.ini')
     cherrypy.config.update(configfile)
     cherrypy.config.update(self.config)
     
     app = cherrypy.tree.mount(root = None, config = configfile)
     app.merge(self.config)
     
     cherrypy.engine.autoreload.files.add(get_absolute_path('core.ini'))
     cherrypy.engine.start()
     log.info('Server started...')
     
     # Start snatch scheduler
     scheduler = Scheduler()
     scheduler.start()
     
     cherrypy.engine.block()