Skip to content

Commit da95c44

Browse files
remote: use more explicit event loop handling
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]>
1 parent 624667c commit da95c44

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

labgrid/remote/coordinator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def __init__(self) -> None:
194194
self.clients: dict[str, ClientSession] = {}
195195
self.load()
196196

197-
self.loop = asyncio.get_event_loop()
197+
self.loop = asyncio.get_running_loop()
198198
self.poll_task = self.loop.create_task(self.poll())
199199

200200
async def _poll_step(self):
@@ -1025,7 +1025,9 @@ def main():
10251025

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

1028-
loop = asyncio.get_event_loop()
1028+
loop = asyncio.new_event_loop()
1029+
asyncio.set_event_loop(loop)
1030+
10291031
cleanup = []
10301032
loop.set_debug(True)
10311033
try:

labgrid/remote/exporter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ def __init__(self, config) -> None:
778778
- Setup loop, name, authid and address
779779
- Join the coordinator as an exporter"""
780780
self.config = config
781-
self.loop = asyncio.get_event_loop()
781+
self.loop = asyncio.get_running_loop()
782782
self.name = config["name"]
783783
self.hostname = config["hostname"]
784784
self.isolated = config["isolated"]
@@ -1061,6 +1061,9 @@ def main():
10611061
print(f"exporter hostname: {config['hostname']}")
10621062
print(f"resource config file: {config['resources']}")
10631063

1064+
loop = asyncio.new_event_loop()
1065+
asyncio.set_event_loop(loop)
1066+
10641067
asyncio.run(amain(config), debug=bool(args.debug))
10651068

10661069
if reexec:

0 commit comments

Comments
 (0)