Running multiple tensorflow sessions concurrently
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Running multiple TensorFlow sessions concurrently is useful for parallel model training, hyperparameter search, and serving multiple models. In TensorFlow 1.x, each tf.Session can run in its own thread or process with separate graphs. In TensorFlow 2.x, eager execution removed the session API, but you can still run multiple models concurrently using Python threading, multiprocessing, or tf.distribute strategies. GPU memory management is the main challenge — multiple sessions competing for GPU memory without proper configuration will crash with out-of-memory errors.
TensorFlow 1.x: Multiple Sessions
Each session must have its own tf.Graph. Using the same graph across sessions without synchronization causes race conditions.
GPU Memory Configuration
By default, TensorFlow allocates all available GPU memory to the first session. Configure memory growth to allow multiple sessions to share the GPU:
TensorFlow 2.x: Concurrent Model Training
Multiprocessing for True Parallelism
Python's GIL limits threading to one CPU thread at a time. For CPU-bound work, use multiprocessing:
Each process has its own Python interpreter and TensorFlow runtime, avoiding GIL contention.
Using CUDA_VISIBLE_DEVICES
Assign specific GPUs to each process:
Set CUDA_VISIBLE_DEVICES before importing TensorFlow in each process.
Common Pitfalls
- Not configuring GPU memory growth: TensorFlow allocates all GPU memory by default. Two sessions on the same GPU without
set_memory_growth(True)orper_process_gpu_memory_fractioncauses the second to crash withCUDA_ERROR_OUT_OF_MEMORY. - Sharing a graph across threads without synchronization: In TF 1.x, two sessions running operations on the same graph concurrently can corrupt shared state. Each thread should use its own
tf.Graphandtf.Session. - Setting
CUDA_VISIBLE_DEVICESafter importing TensorFlow: TensorFlow reads GPU configuration at import time. Setting the environment variable afterimport tensorflowhas no effect. Set it before the import in each subprocess. - Using threading for CPU-bound training: Python's GIL serializes CPU-bound threads. For models that train primarily on CPU, use
multiprocessinginstead ofthreadingfor actual parallelism. - Ignoring TF 2.x eager execution: In TF 2.x,
tf.Sessionis removed by default. Code that createstf.Sessionobjects directly will fail. Usetf.compat.v1.Sessionwithdisable_eager_execution()for legacy code, or restructure to use Kerasmodel.fit()directly.
Summary
- TF 1.x supports multiple concurrent sessions — each must have its own
tf.Graph - TF 2.x uses eager execution — run concurrent models with threading or multiprocessing using Keras APIs
- Configure GPU memory growth with
set_memory_growth(True)to prevent OOM errors when sharing a GPU - Use
multiprocessingfor CPU-bound training to bypass Python's GIL - Assign GPUs to processes with
CUDA_VISIBLE_DEVICESbefore importing TensorFlow - For distributed training across multiple GPUs, prefer
tf.distribute.MirroredStrategyover manual session management
Related reading
- running nvidia-docker on Windows 10 WSL2
- Running tf.mod and tf.floor_div in tensorflow in GPU
- RuntimeError Attempting to deserialize object on a CUDA device
- RuntimeError Expected 4-dimensional input for 4-dimensional weight 32 3 3, but got 3-dimensional input of size 3, 224, 224 instead?
- Running session using tensorflow c api is significantly slower than using python
- Running Tensorflow graph multiple times over different input parameters what kind of loop is efficient?
- Running Tensorflow in Jupyter Notebook
- Running TensorFlow on a Slurm Cluster?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.