[Python] Python error: model adapter API call error: pyodbc.OperationalError '[08004] (-1036)...

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I am running a model adapter through an app. After activating the app, I am attempting to test the API calls. The first API test run is working properly. Testing the API calls the second time (keeping the app active) is throwing errors. The error is in one of the intermediate calls where the adapter is attempting to connect to an access database.

    This API call works in the following way:


    • Description: Call to retrieve the status of the model


    • Returns:

      1. model_run_id


      2. model state: ACCEPTED, PENDING, QUEUED, RUNNING, SUCCEEDED, ERROR


      3. reason: description in case of state ERROR

    The API call is coded as below:

    @api.route("/status/<model_run_id>")
    class Status(MethodView):

    @api.response(200, ModelRunInfo.Schema())
    def get(self, model_run_id: str):
    res = opera.status(model_run_id=model_run_id)
    return jsonify(res)


    The API calls the following method within the adapter (Let's call this Model_X wrapping model_Y):

    Class Model_X(Model_Y):
    ..
    ..
    ..
    def status(self, model_run_id: str):
    if model_run_id in self.model_run_dict:
    if not executor.futures.done(model_run_id):
    return ModelRunInfo(
    state=self.model_run_dict[model_run_id].state,
    model_run_id=model_run_id,
    reason=f"executor.futures._state: {executor.futures._state(model_run_id)}"
    )
    else:
    #print("executor.futures._state: ", executor.futures._state(model_run_id)) # FINISHED
    future = executor.futures.pop(model_run_id)
    executor.futures.add(model_run_id, future) # Put it back on again, so it can be retreived in results
    model_run_info = future.result()
    if model_run_info.result is not None:
    print('Model execution result:', model_run_info.result)
    else:
    logger.warning("No result in model_run_info variable")
    return model_run_info
    else:
    return ModelRunInfo(
    model_run_id=model_run_id,
    state=ModelState.ERROR,
    reason="Error in ESSIM.status(): model_run_id unknown"
    )


    Within the Model_Y, the same method is defined as follows:

    Class Model_Y:
    ..
    ..
    ..
    def status(self, model_run_id: str):
    if model_run_id in self.model_run_dict:
    # Dummy behaviour: Query status once, to let finish model
    self.model_run_dict[model_run_id].state = ModelState.SUCCEEDED

    return ModelRunInfo(
    state=self.model_run_dict[model_run_id].state,
    model_run_id=model_run_id,
    )
    else:
    return ModelRunInfo(
    model_run_id=model_run_id,
    state=ModelState.ERROR,
    reason="Error in Model.status(): model_run_id unknown"
    )



    The method to connect to the Microsoft Access Driver is coded below:

    def connect_to_access(self, access_file: str):
    odbc_string = r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=' + access_file + ';'

    print(f"Connecting to database {access_file}")
    connection_url = sqlalchemy.engine.URL.create(
    "access+pyodbc",
    query={"odbc_connect": odbc_string}
    )
    # pyodbc.connect(odbc_string)
    self.engine = sqlalchemy.create_engine(connection_url, pool_pre_ping=True)
    self.engine.connect()
    self.conn = self.engine.raw_connection()
    self.cursor = self.conn.cursor()


    The method to disconnect Access is coded below:

    def disconnect(self):
    log.info("Disconnecting database")
    self.cursor.close()
    self.conn.close()


    The short description of the error is as follows: OperationalError: (pyodbc.OperationalError) ('08004', '[08004] [Microsoft][ODBC Microsoft Access Driver] Too many client tasks. (-1036) (SQLDriverConnect)')\n(Background on this error at: https://sqlalche.me/e/20/e3q8)"

    I have attempted to look into stack overflow to find similar issues. However, I find that my connection uses a different package. I have also followed the link in the error description. However, I still keep on getting the same error.

    I intend to run the API calls without any issues as the app is supposed to always run in the background. Currently, I have to stop the app completely and restart it again and again to not get errors in the test API runs.

    FYI: I did not create this app or the API calls. I am a user of the model adapter. Other relevant information: My environment has both 32 and 64-bit Microsoft Access drivers, with 64-bit installed. Both the model and the adapter are hosted in my own environment. When I tested the connect_to_access and disconnect methods (i.e., without involving API calls), I did not get any errors.

    Continue reading...

Compartilhe esta Página