Skip to content

Commit

Permalink
remote: use more explicit event loop handling
Browse files Browse the repository at this point in the history
Calling asyncio.get_event_loop() with no current event loop is deprecated
since Python 3.10 and will be an error in some future Python release
[1].

Whenever we don't expect to run with an event loop, create one
explicitly. In coroutine and callbacks from asynchronous code, use the
more explicit asyncio.get_running_loop() to get the loop.

Note that this does not work in
labgrid.resources.ethernetport.EthernetPortManager: This code is usually
not called in coroutines and callbacks from asynchronous code, so
asyncio.get_running_loop() does not work there. So stick to
asyncio.get_event_loop() there and just expect to be called with a
running event loop (which is the non-deprecated use case for this
function).
Users that do not have an event loop running will see a justified
DeprecationWarning with Python >= 3.12 and an error in some future Python
version.

[1] https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop

Signed-off-by: Bastian Krause <[email protected]>
  • Loading branch information
Bastian-Krause committed Aug 8, 2024
1 parent 40132f4 commit d915c12
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions labgrid/remote/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def __init__(self) -> None:
self.clients: dict[str, ClientSession] = {}
self.load()

self.loop = asyncio.get_event_loop()
self.loop = asyncio.get_running_loop()
self.poll_task = self.loop.create_task(self.poll())

async def _poll_step(self):
Expand Down Expand Up @@ -1025,7 +1025,9 @@ def main():

logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)

loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

cleanup = []
loop.set_debug(True)
try:
Expand Down
5 changes: 4 additions & 1 deletion labgrid/remote/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def __init__(self, config) -> None:
- Setup loop, name, authid and address
- Join the coordinator as an exporter"""
self.config = config
self.loop = asyncio.get_event_loop()
self.loop = asyncio.get_running_loop()
self.name = config["name"]
self.hostname = config["hostname"]
self.isolated = config["isolated"]
Expand Down Expand Up @@ -1061,6 +1061,9 @@ def main():
print(f"exporter hostname: {config['hostname']}")
print(f"resource config file: {config['resources']}")

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

asyncio.run(amain(config), debug=bool(args.debug))

if reexec:
Expand Down

0 comments on commit d915c12

Please sign in to comment.