원격 서비스 라이브러리

이 모듈에는 표준 요청 및 응답 모델을 준수하는 원격 서비스 빌드에 유용하게 사용할 수 있는 클래스가 포함되어 있습니다. 이 모델을 준수하려면 서비스는 다음 클래스와 같아야 합니다.

# Each service instance only handles a single request and is then discarded.
# Make these objects light weight.
class Service(object):

    # It must be possible to construct service objects without any parameters.
    # If your constructor needs extra information you should provide a
    # no-argument factory function to create service instances.
    def __init__(self):
        ...

    # Each remote method must use the 'remote' decorator, passing the request
    # and response message types.  The remote method itself must take a single
    # parameter which is an instance of RequestMessage and return an instance
    # of ResponseMessage.
    @method(RequestMessage, ResponseMessage)
    def remote_method(self, request):
        # Return an instance of ResponseMessage.

    # A service object may optionally implement a 'initialize_request_state'
    # method that takes as a parameter a single instance of a RequestState.  If
    # a service does not implement this method it will not receive the request
    # state.
    def initialize_request_state(self, state):
        ...

Service 클래스는 위의 기능을 제공하는 편리한 기본 클래스입니다. 이 클래스는 서비스의 필수 메서드 및 선택적 메서드를 모두 구현합니다. 영구 전역 상태를 새 서비스 인스턴스에 전달할 수 있는 팩토리 함수를 생성하기 위한 편의 메서드도 포함되어 있습니다.

원격 데코레이터는 RPC를 서비스할 클래스 메서드를 선언하는 데 사용됩니다. 하지만 이 데코레이터는 소켓 및 다양한 기본 RPC 프로토콜을 전달하는 작업을 담당하지 않습니다. 데코레이터는 올바른 요청 유형을 사용하는지 확인하지만 초기화를 확인하지는 않습니다.

원격 데코레이터가 메서드에 사용되는 경우 래퍼 메서드에 '원격' 속성이 연결됩니다. 이 속성에는 메서드의 구현에 필요한 request_typeresponse_type이 포함됩니다.

원격 데코레이터는 그 자체로는 원격 메서드의 서브클래스화를 지원하지 않습니다. 서비스를 확장하려면 서브클래스 메서드를 사용하여 다시 데코레이션합니다. 예:

class MyService(Service):

    @method(DoSomethingRequest, DoSomethingResponse)
    def do_something(self, request):
        ... implement do-something ...

class MyBetterService(MyService):

    @method(DoSomethingRequest, DoSomethingResponse)
    def do_something(self, request):
        response = super(MyBetterService, self).do_something.remote.method(request)
        ... do something with response ...
        return response