Exemplo n.º 1
0
class Item(object):
    def __init__(self, name, value):
        super(Item, self).__init__()
        self._name = name
        self._value = value
        self.wrlock = Lock()

    @property
    def name(self):
        return self._name

    #Item的属性,相当于read
    @property
    def value(self):
        self.wrlock.acquire_read()
        print("acquire")
        res = self._value
        self.wrlock.release()
        print("release")
        return res

    @value.setter
    def value(self, value):
        self.write(value)

    def read(self):
        pass

    def write(self, value):
        self.wrlock.acquire_write()
        print("acquire write")
        self._value = value
        time.sleep(3)
        self.wrlock.release()
        print("release")

    def withdraw(self, value):
        time.sleep(2)
        self.wrlock.acquire_write()
        self._value = str(int(self.value) - int(value))
        time.sleep(1)
        self.wrlock.release()
Exemplo n.º 2
0
class HttpServer(BaseParameterServer):
    """HttpServer

    Flask HTTP server. Defines two routes, `/parameters` to GET current
    parameters held by this server, and `/update` which can be used to
    POST updates.
    """
    def __init__(self,
                 model,
                 optimizer,
                 mode,
                 port=4000,
                 debug=True,
                 threaded=True,
                 use_reloader=True):
        """Initializes and HTTP server from a serialized Keras model, elephas optimizer,
        a parallelisation mode and a port to run the Flask application on. In
        hogwild mode no read- or write-locks will be acquired, in asynchronous
        mode this is the case.

        :param model: Serialized Keras model
        :param optimizer: Elephas optimizer
        :param mode: parallelization mode, either `asynchronous` or `hogwild`
        :param port: int, port to run the application on
        :param debug: boolean, Flask debug mode
        :param threaded: boolean, Flask threaded application mode
        :param use_reloader: boolean, Flask `use_reloader` argument
        """

        self.master_network = dict_to_model(model)
        self.mode = mode
        self.master_url = None
        self.optimizer = optimizer

        self.port = port

        if is_running_in_notebook():
            self.threaded = False
            self.use_reloader = False
            self.debug = False
        else:
            self.debug = debug
            self.threaded = threaded
            self.use_reloader = use_reloader

        self.lock = Lock()
        self.pickled_weights = None
        self.weights = self.master_network.get_weights()

        self.server = Process(target=self.start_flask_service)

    def start(self):
        self.server.start()
        self.master_url = determine_master(self.port)

    def stop(self):
        self.server.terminate()
        self.server.join()

    def start_flask_service(self):
        """Define Flask parameter server service.

        This HTTP server can do two things: get the current model
        parameters and update model parameters. After registering
        the `parameters` and `update` routes, the service will
        get started.

        """
        app = Flask(__name__)
        self.app = app

        @app.route('/')
        def home():
            return 'Elephas'

        @app.route('/parameters', methods=['GET'])
        def handle_get_parameters():
            if self.mode == 'asynchronous':
                self.lock.acquire_read()
            self.pickled_weights = pickle.dumps(self.weights, -1)
            pickled_weights = self.pickled_weights
            if self.mode == 'asynchronous':
                self.lock.release()
            return pickled_weights

        @app.route('/update', methods=['POST'])
        def handle_update_parameters():
            delta = pickle.loads(request.data)
            if self.mode == 'asynchronous':
                self.lock.acquire_write()

            if not self.master_network.built:
                self.master_network.build()

            def base_constraint(a):
                return a

            constraints = [base_constraint for _ in self.weights]
            self.weights = self.optimizer.get_updates(self.weights,
                                                      constraints, delta)
            if self.mode == 'asynchronous':
                self.lock.release()
            return 'Update done'

        # self.app.run(host='0.0.0.0', debug=self.debug, port=self.port,
        #              threaded=self.threaded, use_reloader=self.use_reloader)
        from werkzeug.serving import run_simple
        run_simple(self.master_url.split(':')[0], self.port, self.app)