"git@developer.sourcefind.cn:OpenDAS/vision.git" did not exist on "7aef257bf3dd289286ee9b4ca2355cb5e34c8acd"
Unverified Commit ed1948b5 authored by Lingfan Yu's avatar Lingfan Yu Committed by GitHub
Browse files

[BugFix] Make program thread-local to support multi-threading (#338)

* make program thread local

* doc string
parent 19e1b140
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from __future__ import absolute_import from __future__ import absolute_import
from contextlib import contextmanager from contextlib import contextmanager
import threading
from .registry import IR_REGISTRY from .registry import IR_REGISTRY
...@@ -44,18 +45,30 @@ class Prog(object): ...@@ -44,18 +45,30 @@ class Prog(object):
for exe in self.execs: for exe in self.execs:
self.pprint_exe(exe) self.pprint_exe(exe)
class CurrentProgram(threading.local):
"""Thread local storage to keep the reference of current thread's program"""
def __init__(self):
super(CurrentProgram, self).__init__()
self.prog = None
def get_prog(self):
"""Get program"""
return self.prog
def set_prog(self, program):
"""Set program"""
self.prog = program
# current program # current program
CURRENT_PROG = None CURRENT_PROG = CurrentProgram()
def get_current_prog(): def get_current_prog():
"""Get the current program.""" """Get the current program."""
global CURRENT_PROG return CURRENT_PROG.get_prog()
return CURRENT_PROG
def set_current_prog(program): def set_current_prog(program):
"""Set the current program.""" """Set the current program."""
global CURRENT_PROG CURRENT_PROG.set_prog(program)
CURRENT_PROG = program
@contextmanager @contextmanager
def prog(): def prog():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment