Skip to content

Commit

Permalink
pw_transfer: Set event loop in non-main thread
Browse files Browse the repository at this point in the history
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 <[email protected]>
Commit-Queue: Wyatt Hepler <[email protected]>
Reviewed-by: Wyatt Hepler <[email protected]>
Commit-Queue: Carlos Chinchilla <[email protected]>
  • Loading branch information
ChinchillaWithGoggles authored and CQ Bot Account committed Jul 1, 2022
1 parent c50e44f commit de64d37
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pw_transfer/py/pw_transfer/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit de64d37

Please sign in to comment.