From de64d37ce9ba9239bb0a79644604f083dfd69b40 Mon Sep 17 00:00:00 2001 From: Carlos Chinchilla Date: Fri, 1 Jul 2022 17:46:50 +0000 Subject: [PATCH] pw_transfer: Set event loop in non-main thread When the Python pw_transfer.Manager is created, it creates a new asyncio event loop, which is auto-registered if created in the main thread. However, if the pw_transfer.Manager is created in a non-main thread, this event loop is not registered, and the pw_transfer.Manager constructor will fail with "RuntimeError: There is no current event loop in thread 'THREAD_NAME'.". Check if in non-main thread to register the new event loop. Change-Id: Ia22260a0ecd5d1921957efd6d42f534cfbdd5676 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/100415 Pigweed-Auto-Submit: Carlos Chinchilla Commit-Queue: Wyatt Hepler Reviewed-by: Wyatt Hepler Commit-Queue: Carlos Chinchilla --- pw_transfer/py/pw_transfer/client.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pw_transfer/py/pw_transfer/client.py b/pw_transfer/py/pw_transfer/client.py index 27c5459598..30d7d86a85 100644 --- a/pw_transfer/py/pw_transfer/client.py +++ b/pw_transfer/py/pw_transfer/client.py @@ -74,6 +74,9 @@ def __init__(self, self._write_stream: Optional[BidirectionalStreamingCall] = None self._loop = asyncio.new_event_loop() + # If constructed from non-main thread, set the event loop. + if threading.current_thread() is not threading.main_thread(): + asyncio.set_event_loop(self._loop) # Queues are used for communication between the Manager context and the # dedicated asyncio transfer thread.