I am working on a C++ application running Tensorflow. The app has to run well with fork()
as the code bellow. However, I have learned that Tensorflow runtime is not fork-safe. The code hangs when it reaches to the Session.run()
command. As I am a beginner in C++, I would like to ask you for suggestion how should I run Tensorflow in this case.
Edit: The object is created in the main thread and fork()
may be called many times.
int main ()
{
Myapp myapp("path_to_model");
switch(fork()) {
case 0: {/* Child */
myapp.get_embedding(some_data);
break;
}
case -1:{ /* Error */
cerr << "Problem forking" << endl;
break;
}
default:
cout << "on the parent process";
break;
}
}
return 0;
}
myapp:
//#ifndef _MYAPP_H_
//#define _MYAPP_H_
#ifndef MYAPP_H_
#define MYAPP_H_
#include <iostream>
#include <tensorflow/core/public/session.h>
using namespace std;
using namespace tensorflow;
class Myapp {
string protobuf_fn; // model filename
public:
Session *sess;
/*constructor with model file name*/
Myapp (string pb_fn){
//init the session and load the graph
TF_CHECK_OK(loadModelpb(pb_fn));
}
/*read trained model file into the session*/
tensorflow::Status loadModelpb(string pb_fn);
/* get an embedding by running Session.run()
* For now, it hangs when gets called on a child process.
*/
Tensor get_embedding(Data some_data) const;
};
#endif /*_MYAPP_H_*/
Thank you very much in advance