code: def main():
      # open a socket if port [+ host] specified in commandline args
      if len(sys.argv) > 1:
          if sys.argv[1] == "server":
              port, server_socket = comm.open_server_socket()
              print(port)
              ## flush is needed for R pipe():
              sys.stdout.flush()
              comm.accept_server_connection(server_socket)
  
          else:
              try:
                  port = int(sys.argv[1])
              except ValueError as e:
                  print("Invalid socket port: " + str(e))
                  sys.exit(1)
              if len(sys.argv) > 2:
                  host = sys.argv[2]
              else:
                  host = "localhost"
              comm.open_socket(port, host)
  
      startup_json = comm.receive()
  
      try:
          startup_msg = comm.json_decode(startup_json)
  
          if "manual" in startup_msg:
              solver_name = startup_msg["manual"]
              manual_request(solver_name)
  
          elif "generate_folds" in startup_msg:
              import optunity.cross_validation as cv
  
              cv_opts = startup_msg["generate_folds"]
              fold_request(cv_opts)
  
          elif "make_solver" in startup_msg:
              solver_config = startup_msg["make_solver"]
              replacements = comm._find_replacements(
                  _illegal_keys, solver_config
              )
              solver_config = comm._replace_keys(
                  solver_config, replacements
              )
              make_solver(solver_config)
  
          elif "maximize" in startup_msg or "minimize" in startup_msg:
              if startup_msg.get("maximize", False):
                  kwargs = startup_msg["maximize"]
                  solve_fun = optunity.maximize
              else:
                  kwargs = startup_msg["minimize"]
                  solve_fun = optunity.minimize
  
              max_or_min(
                  solve_fun,
                  kwargs,
                  startup_msg.get("constraints", {}),
                  startup_msg.get("default", None),
                  startup_msg.get("call_log", None),
              )
  
          elif "optimize" in startup_msg:
              max_evals = startup_msg["optimize"].get("max_evals", 0)
              maximize = startup_msg["optimize"].get("maximize", True)
  
              # sanity check
              if not "solver" in startup_msg:
                  msg = {
                      "error_msg": "No solver specified in startup message."
                  }
                  comm.send(comm.json_encode(msg))
                  print(startup_msg, file=sys.stderr)
                  exit(1)
  
              optimize(
                  startup_msg["solver"],
                  startup_msg.get("constraints", {}),
                  startup_msg.get("default", None),
                  startup_msg.get("call_log", None),
                  maximize,
                  max_evals,
              )
  
          else:  # solving a given problem
              mgr = comm.EvalManager()
              func = optunity.wrap_constraints(
                  comm.make_piped_function(mgr),
                  startup_msg.get("default", None),
                  **startup_msg.get("constraints", {}),
              )
  
              if startup_msg.get("call_log", False):
                  func = optunity.wrap_call_log(
                      func, startup_msg["call_log"]
                  )
              else:
                  func = functions.logged(func)
  
              maximize = startup_msg.get("maximize", True)
  
              # instantiate solver
              try:
                  solver = optunity.make_solver(
                      startup_msg["solver"], **startup_msg["config"]
                  )
              except (ValueError, KeyError):
                  msg = {"error_msg": "Unable to instantiate solver."}
                  comm.send(comm.json_encode(msg))
                  print(startup_msg, file=sys.stderr)
                  exit(1)
              except EOFError:
                  msg = {"error_msg": "Broken pipe."}
                  comm.send(comm.json_encode(msg))
                  exit(1)
  
              # solve and send result
              try:
                  solution, rslt = optunity.optimize(
                      solver, func, maximize, pmap=mgr.pmap
                  )
              except EOFError:
                  msg = {"error_msg": "Broken pipe."}
                  comm.send(comm.json_encode(msg))
                  exit(1)
  
              result = rslt._asdict()
              result["solution"] = solution
              result_json = comm.json_encode(result)
              comm.send(result_json)
              exit(0)
  
      except (ValueError, TypeError, AttributeError) as e:
          msg = {"error_msg": str(e)}
          comm.send(comm.json_encode(msg))
          exit(1)
  
file_path: optunity/optunity/standalone.py
imports: from __future__ import print_function
  import sys
  import keyword
  from .None import communication
  from .None import functions
  import optunity
label_desc: Use image node_id 8 for calling a global function with example usage: main() without return types