0

I am exploring GRPC (C++). Following their examples I am trying to create a server which accepts an image from the client returns the text in the image. I have a python code which accepts an image and a json file describing the bounding box of the text in that image and returns the text in the bounding box.

I am using python C API to use my existing python code(which uses OCR) to extract text. Everything working as expected if I call python function before grpc server is run. But If I call python function in the rpc then python function is not returning anything, it is becoming non responsive after executing some part of the python code (I debugged).

I tested with python snippet which will sleep for 30 sec, it is working. What am I doing wrong? Am I not supposed to call python/ocr from rpc method ? Please give me any direction.

Thank you.

sample code

class ClientImpl final : public ImagToText::Service
{
public:
    explicit ClientImpl()
    {

    }

    Status GetOCRText(ServerContext* context, ServerReader<UploadImageRequest>*reader, ITTResponse *response) override
    {
        //...
        response->set_ocrtext(PythonFun("img.jpg","data.json"));

        return Status::OK;
    }

    string PythonFun(std::string str0,std::string str1)
    {
     //...
    }
}
void RunServer()
{
    std::string server_address("0.0.0.0:50051");
    ClientImpl service;

    ServerBuilder builder;
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    builder.RegisterService(&service);
    std::unique_ptr<Server> server(builder.BuildAndStart());
    std::cout << "Server listening on " << server_address << std::endl;

    server->Wait();
}

int main(int argc,char *argv[])
{
    //Below case is working fine
    //PythonFun("img.jpg","data.json");
    Py_Initialize();

    RunServer();

    Py_Finalize();
    return 0;

}
Raki
  • 329
  • 3
  • 18

1 Answers1

0

I went through Python C API documentation and found the following [https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock][1]

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

/* Perform Python actions here. */
result = CallSomeFunction();
/* evaluate result or handle exception */

/* Release the thread. No Python API allowed beyond this point. */
PyGILState_Release(gstate);

This helped me solving my issue. Hope this helps somebody.

Raki
  • 329
  • 3
  • 18