def run_in_background(self, runnable, name=None): # type: (Function, str) -> None """Run `runnable` in another thread, locking on this DB.""" if name is None: name = runnable.func_name else: runnable.func_name = name print(threading.current_thread()) @functools.wraps(runnable) def locking_wrapper(): print('{} is waiting to run in the background'.format(name)) print(threading.current_thread()) with self: print('{} is running in the background'.format(name)) runnable() print('{} is done running in the background'.format(name)) thread = Thread(target=locking_wrapper) thread.background = True thread.start()