Skip to content

Commit

Permalink
Make thread_coroutine more generally usable.
Browse files Browse the repository at this point in the history
  • Loading branch information
rohinb2 committed Aug 19, 2024
1 parent 7e54396 commit 1a4b9ca
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions runhouse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,11 @@ def run_command_with_password_login(
####################################################################################################


def _thread_coroutine(coroutine, context):
def thread_coroutine(coroutine, context=None):
# Copy contextvars from the parent thread to the new thread
for var, value in context.items():
var.set(value)
if context is not None:
for var, value in context.items():
var.set(value)

# Technically, event loop logic is not threadsafe. However, this event loop is only in this thread.
loop = asyncio.new_event_loop()
Expand All @@ -272,7 +273,7 @@ def wrapper(*args, **kwargs):
# and the resources are cleaned up
with ThreadPoolExecutor() as executor:
future = executor.submit(
_thread_coroutine,
thread_coroutine,
coroutine_func(*args, **kwargs),
contextvars.copy_context(),
)
Expand Down

0 comments on commit 1a4b9ca

Please sign in to comment.