How to run the code under a contextmanager in a separate thread?

Viewed 403

I wanted to have a context manager where i can put some code which to be executed in a separate thread.

So far i couldn't find a way to achieve what i want, best alternative is writing closures and executing closures in a separate thread.

I want something like this

# code runs on main thread
print("this is main thread")

with amazingcontextmanager:
    # code to run in separate thread
    print("this is not main thread")

edit: let me try to ask my question again

@contextlib.contextmanager
def amazingcontextmanager():
    try:
        yield
    finally:
        print("thread done")

I want yield to execute in a new thread. Basically whatever i put under contextmanager should be executed in a separate thread.

0 Answers
Related