-
Notifications
You must be signed in to change notification settings - Fork 536
Multithreading
Eli Belash edited this page Aug 31, 2019
·
15 revisions
Tensorflow.NET is thread-safe, our multithreading model is thread-wide Session and Graph; meaning tf.get_default_graph/session()
are unique to the thread they are executed in.
We chose this model because a dominant portion of our api does not accept Graph
as an argument, instead
it accesses tf.get_default_graph()
to initialize an Operation
in it.
This allows cleaner and similar code to Python and still having complete isolation between threads.
- Initialize sessions and graphs.
- Call
your_session.run(...)
parallely and in separate threads regardless to the defaults in the current thread.
- When writing a model, it has to be done in the same thread unless
yourgraph.as_default()
is called in a different thread.
For example : You can't start a model and then continue it in a Task unless you callgraph.as_default()
andsession.as_default()
before doing so. - Tensorflow's c_api for the most part is thread-safe, calls of
status.Check()
sometimes to be inside a process-wide lock, for example:lock (Locks.ProcessWide) { var status = new Status(); c_api.someapicall(); status.Check(true); }