コード例 #1
0
 def process(self, data):
     self._worker_task_return_queue = self._current_task_client
     try:
         return self._process(data)
     except:
         logger.error("Scenario tree server %s caught an exception of type "
                      "%s while processing a task. Going idle." %
                      (self.WORKERNAME, sys.exc_info()[0].__name__))
         traceback.print_exception(*sys.exc_info())
         self._worker_error = True
         return TaskProcessingError(traceback.format_exc())
コード例 #2
0
ファイル: server_pyro.py プロジェクト: CanLi1/pyomo-1
 def process(self, data):
     self._worker_task_return_queue = self._current_task_client
     try:
         # The only reason we are go through this much
         # effort to deal with the serpent serializer
         # is because it is the default in Pyro4.
         if using_pyro4 and \
            (Pyro4.config.SERIALIZER == 'serpent'):
             if six.PY3:
                 assert type(data) is dict
                 assert data['encoding'] == 'base64'
                 data = base64.b64decode(data['data'])
             else:
                 assert type(data) is unicode
                 data = str(data)
         return pickle.dumps(self._process(pickle.loads(data)))
     except:
         logger.error("Scenario tree server %s caught an exception of type "
                      "%s while processing a task. Going idle." %
                      (self.WORKERNAME, sys.exc_info()[0].__name__))
         traceback.print_exception(*sys.exc_info())
         self._worker_error = True
         return pickle.dumps(TaskProcessingError(traceback.format_exc()))
コード例 #3
0
ファイル: pyro_mip_server.py プロジェクト: zypher22/pyomo
    def process(self, data):
        self._worker_task_return_queue = self._current_task_client
        data = pyutilib.misc.Bunch(**data)

        if hasattr(data, 'action') and \
           data.action == 'Pyomo_pyro_mip_server_shutdown':
            print("Received shutdown request")
            self._worker_shutdown = True
            return

        time_start = time.time()
        with pyutilib.services.TempfileManager.push():
            #
            # Construct the solver on this end, based on the input
            # type stored in "data.opt".  This is slightly more
            # complicated for asl-based solvers, whose real executable
            # name is stored in data.solver_options["solver"].
            #
            with SolverFactory(data.opt) as opt:

                if opt is None:
                    self._worker_error = True
                    return TaskProcessingError(
                        "Problem constructing solver `" + data.opt + "'")

                # here is where we should set any options required by
                # the solver, available as specific attributes of the
                # input data object.
                solver_options = data.solver_options
                del data.solver_options
                for key, value in solver_options.items():
                    setattr(opt.options, key, value)

                problem_filename_suffix = os.path.split(data.filename)[1]
                temp_problem_filename = \
                    pyutilib.services.TempfileManager.\
                    create_tempfile(suffix="."+problem_filename_suffix)

                with open(temp_problem_filename, 'w') as f:
                    f.write(data.file)

                if data.warmstart_filename is not None:
                    warmstart_filename_suffix = \
                        os.path.split(data.warmstart_filename)[1]
                    temp_warmstart_filename = \
                        pyutilib.services.TempfileManager.\
                        create_tempfile(suffix="."+warmstart_filename_suffix)
                    with open(temp_warmstart_filename, 'w') as f:
                        f.write(data.warmstart_file)
                    assert opt.warm_start_capable()
                    assert (('warmstart' in data.kwds) and \
                            data.kwds['warmstart'])
                    data.kwds['warmstart_file'] = temp_warmstart_filename

                now = datetime.datetime.now()
                if self._verbose:
                    print(
                        str(now) + ": Applying solver=" + data.opt +
                        " to solve problem=" + temp_problem_filename)
                    sys.stdout.flush()
                results = opt.solve(temp_problem_filename, **data.kwds)
                assert results._smap_id is None
                # NOTE: This results object contains solutions,
                # because no model is provided (just a model file).
                # Also, the results._smap_id value is None.

        results.pyomo_solve_time = time.time() - time_start

        now = datetime.datetime.now()
        if self._verbose:
            print(
                str(now) + ": Solve completed - number of solutions=" +
                str(len(results.solution)))
            sys.stdout.flush()

        # PYTHON3 / PYRO4 Fix
        # The default serializer in Pyro4 is not pickle and does not
        # support user defined types (e.g., the results object).
        # Therefore, we pickle the results object before sending it
        # over the wire so the user does not need to change the Pyro
        # serializer.
        results = pickle.dumps(results, protocol=pickle.HIGHEST_PROTOCOL)

        if using_pyro4:
            #
            # The standard bytes object returned by pickle.dumps must be
            # converted to base64 to avoid errors sending over the
            # wire with Pyro4. Also, the base64 bytes must be wrapped
            # in a str object to avoid a different set of Pyro4 errors
            # related to its default serializer (Serpent)
            if six.PY3:
                results = str(base64.encodebytes(results))
            else:
                results = base64.encodestring(results)

        return results