7.2 Asynchronous Problem Submission

This tutorial shows how to

  • submit a job to the OptServer,
  • start the optimization job running MOSEK on server side,
  • closing the connection to the server and
  • connect again and retrieve te solution and log.

The optimization problem is assumed to be stored in a file using one of the available file format. The connection is managed using http Python module, and it is assumed to be establish successfully.

First of all, the problem is submitted to the OptServer by a POST operation

Listing 7.6 How to submit a job to the OptServer.
            con.request("POST",'/submit', dataf)
            resp = con.getresponse()

Note that this operation is identical to the synchronuous case.

If no errors have occourred, a request for running the optimizer can be sent

Listing 7.7 How to run an optimization job with OptServer.
            con.request("GET","/solve-background?token="+token)
            resp = con.getresponse()

The program regains control immediately. The connection is then closed and reopened, to make sure client and server are working asynchronously.

If no errors have happened, the results can be retrieved

Listing 7.8 How to get the results.
            con.request("GET","/solution?token="+token)
            resp = con.getresponse()

            check_status(resp)
            
            res = resp.getheader('X-Mosek-Res-Code',None)
            trm = resp.getheader('X-Mosek-Trm-Code',None)

            print("\tMOSEK response: %s" % res)
            print("\t      trm resp: %s" % trm)
            if resp.status == http.client.OK:
                print("Solution:")
                print(resp.read().decode('ascii',errors='ignore'))

The log is readily available as well

Listing 7.9 How to retrieve the log for a job on OptServer.
            con.request("GET","/log?token="+token)
            resp = con.getresponse()

            if resp.status == http.client.OK:
                print(resp.read().decode('utf-8',errors='ignore'))

The whole example is in Listing 7.10.

Listing 7.10 How to submit a job and solve the problem asynchronously. Click here to download.
import http.client
import sys

def check_status(resp):

    print("\tHTTPResponse: %s / %s" % (resp.status,resp.reason))
    for k,v in  resp.getheaders():
        print("\t%s: %s" % (k,v))
            
    if resp.status not in [http.client.OK, http.client.NO_CONTENT]:
        print("An error occourred!")
        sys.exit(1)

if __name__ == '__main__':
    host = sys.argv[1]
    port = int(sys.argv[2])

    probfile = sys.argv[3]

    token=[]
    con = http.client.HTTPConnection(host,port)
    try:
        with open(probfile,'rb') as dataf:
            ## Submit job
            print("POST /submit")
            con.request("POST",'/submit', dataf)
            resp = con.getresponse()
            check_status(resp)

            token = resp.read().decode('ascii')
            
            ## Start solving end close connection
            print("GET /solve-background")
            con.request("GET","/solve-background?token="+token)
            resp = con.getresponse()
            check_status(resp)

    finally:
        con.close()
        print("connection closed")


    con = http.client.HTTPConnection(host,port)
    print("connection open")
    
    try:
            print("GET /solution")
            con.request("GET","/solution?token="+token)
            resp = con.getresponse()

            check_status(resp)
            
            res = resp.getheader('X-Mosek-Res-Code',None)
            trm = resp.getheader('X-Mosek-Trm-Code',None)

            print("\tMOSEK response: %s" % res)
            print("\t      trm resp: %s" % trm)
            if resp.status == http.client.OK:
                print("Solution:")
                print(resp.read().decode('ascii',errors='ignore'))

            print("GET /log")
            con.request("GET","/log?token="+token)
            resp = con.getresponse()

            if resp.status == http.client.OK:
                print(resp.read().decode('utf-8',errors='ignore'))
    finally:
        con.close()