Skip to content

Commit 4fa8999

Browse files
authored
fix thread local (#86)
1 parent fa47442 commit 4fa8999

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

python/decord/bridge/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
_CURRENT_BRIDGE = threading.local()
1919
_CURRENT_BRIDGE.type = 'native'
20+
_GLOBAL_BRIDGE_TYPE = 'native' # child threads will derive from the global type but not overwrite
2021

2122
def reset_bridge():
2223
_CURRENT_BRIDGE.type = 'native'
24+
if threading.current_thread().name == 'MainThread':
25+
_GLOBAL_BRIDGE_TYPE = 'native'
2326

2427
def set_bridge(new_bridge):
2528
assert isinstance(new_bridge, str), (
@@ -28,11 +31,17 @@ def set_bridge(new_bridge):
2831
"valid bridges: {}".format(_BRIDGE_TYPES.keys()))
2932
global _CURRENT_BRIDGE
3033
_CURRENT_BRIDGE.type = new_bridge
34+
if threading.current_thread().name == 'MainThread':
35+
_GLOBAL_BRIDGE_TYPE = new_bridge
3136

3237
def bridge_out(native_arr):
38+
if not hasattr(_CURRENT_BRIDGE, 'type'):
39+
_CURRENT_BRIDGE.type = _GLOBAL_BRIDGE_TYPE
3340
return _BRIDGE_TYPES[_CURRENT_BRIDGE.type][0](native_arr)
3441

3542
def bridge_in(arr):
43+
if not hasattr(_CURRENT_BRIDGE, 'type'):
44+
_CURRENT_BRIDGE.type = _GLOBAL_BRIDGE_TYPE
3645
return _BRIDGE_TYPES[_CURRENT_BRIDGE.type][1](arr)
3746

3847
class _BridgeScope(object):
@@ -42,6 +51,8 @@ def __init__(self, bridge_type='native'):
4251

4352
def __enter__(self):
4453
global _CURRENT_BRIDGE
54+
if not hasattr(_CURRENT_BRIDGE, 'type'):
55+
_CURRENT_BRIDGE.type = _GLOBAL_BRIDGE_TYPE
4556
try:
4657
self._prev = _CURRENT_BRIDGE.type
4758
except AttributeError:

tests/python/unittests/test_bridges.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ def test_tvm_bridge():
5959
except ImportError:
6060
print('Skip test tvm bridge as tvm is not found')
6161

62+
def test_threaded_bridge():
63+
# issue #85
64+
from decord import cpu, gpu
65+
from multiprocessing.dummy import Pool as ThreadPool
66+
67+
video_paths = [
68+
os.path.expanduser('~/Dev/decord/examples/flipping_a_pancake.mkv'), #list of paths to video
69+
]
70+
71+
def process_path(path):
72+
vr = VideoReader(path, ctx=cpu(0))
73+
74+
for i in range(len(vr)):
75+
frame = vr[i]
76+
77+
pool = ThreadPool(1)
78+
pool.map(process_path, video_paths)
79+
6280
if __name__ == '__main__':
6381
import nose
6482
nose.runmodule()

0 commit comments

Comments
 (0)