# Threading on TX2 using Tensorrt

**URL:** <https://forums.developer.nvidia.com/t/threading-on-tx2-using-tensorrt/123965>\
**Category:** TensorRT\
**Created:** [May 20, 2020, 5:52am UTC](https://forums.developer.nvidia.com/t/threading-on-tx2-using-tensorrt/123965 "2020-05-20T05:52:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![794836749](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/794836749/32/11704_2.png) [@794836749](https://forums.developer.nvidia.com/u/794836749)\
**Post date:** [May 20, 2020, 5:52am UTC](https://forums.developer.nvidia.com/t/threading-on-tx2-using-tensorrt/123965/1 "2020-05-20T05:52:54Z")

</div>

## Description

Hi all,  
I wanna to create 4 threadings to use model.engine separately. Such as threading 1 can use model.engine for inference and threading 2 , threading 3, threading 4 can do inference too using python

When I put it in threading 1, the program will shut down.

```auto
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "thread_engine_4.py", line 124, in run
    h_input, d_input, h_output, d_output = allocate_buffers(engine)
  File "thread_engine_4.py", line 80, in allocate_buffers
    h_input = cuda.pagelocked_empty(trt.volume(engine.get_binding_shape(0)), dtype= trt.nptype(DTYPE))
pycuda._driver.LogicError: explicit_context_dependent failed: invalid device context - no currently active context?

```

If I add these in threading

```auto
cuda.init()
device = cuda.Device(1)
ctx = device.make_context()

```

It can do inference .  
How can I do this in threading 2, threading 3 and threading4?  
Many thanks

## Environment

**TensorRT Version** : 7  
**GPU Type** : TX2  
**CUDA Version** : Jetpack4.4  
**CUDNN Version** : Jetpack4.4  
**Operating System + Version** : Ubuntu18  
**Python Version (if applicable)**: 3.6  
**TensorFlow Version (if applicable)**: tf1.15.0

---

<div class="post-metadata">

**Author:** ![SunilJB](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/suniljb/32/14043_2.png) [@SunilJB](https://forums.developer.nvidia.com/u/SunilJB)\
**Post date:** [May 20, 2020, 6:41am UTC](https://forums.developer.nvidia.com/t/threading-on-tx2-using-tensorrt/123965/2 "2020-05-20T06:41:06Z")

</div>

Please refer to below thread safety guidelines:  
[https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-710-ea/best-practices/index.html#thread-safety](https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-710-ea/best-practices/index.html#thread-safety)

Thanks
