Skip to content

Commit

Permalink
Use asyncio.run() and asyncio.get_running_loop()
Browse files Browse the repository at this point in the history
  • Loading branch information
SaladDais committed Jan 10, 2024
1 parent 8f2ade1 commit 0778442
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 15 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ async def amain():
print(await viewer_control_api.get("Global", "StatsPilotFile"), file=sys.stderr)


loop = asyncio.new_event_loop()
loop.run_until_complete(amain())
asyncio.run(amain())
```

If you just want to play around with the LEAP APIs:
Expand Down
3 changes: 1 addition & 2 deletions examples/simple_leap_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ async def amain():


def main():
loop = asyncio.new_event_loop()
loop.run_until_complete(amain())
asyncio.run(amain())


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion src/outleap/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def _pump_messages_forever():

# Should naturally stop on its own when disconnect is called by virtue of
# the incomplete read.
self._msg_pump_task = asyncio.get_event_loop().create_task(_pump_messages_forever())
self._msg_pump_task = asyncio.create_task(_pump_messages_forever())

def disconnect(self) -> None:
"""Close the connection and clean up any pending request futures"""
Expand Down
7 changes: 1 addition & 6 deletions src/outleap/scripts/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ async def amain():
pass


def agent_main():
loop = asyncio.new_event_loop()
loop.run_until_complete(amain())


if __name__ == "__main__":
multiprocessing.freeze_support()
agent_main()
asyncio.run(amain())
4 changes: 2 additions & 2 deletions src/outleap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def _make_hacky_threaded_stdio_rw() -> Tuple[asyncio.StreamReader, asyncio
#
# TODO: Currently we also use this if we're reading from a non-pipe file descriptor on POSIX
# platforms, but that's probably unnecessary.
loop = asyncio.get_event_loop()
loop = asyncio.get_running_loop()
reader = asyncio.StreamReader(limit=_READER_BUFFER_LIMIT)
protocol = HackySTDIOProtocol()
transport = HackySTDIOTransport()
Expand Down Expand Up @@ -157,7 +157,7 @@ async def connect_stdin_stdout() -> Tuple[asyncio.StreamReader, asyncio.StreamWr
need_stdin_hack = True
if need_stdin_hack:
return await _make_hacky_threaded_stdio_rw()
loop = asyncio.get_event_loop()
loop = asyncio.get_running_loop()
reader = asyncio.StreamReader(limit=_READER_BUFFER_LIMIT)
protocol = asyncio.StreamReaderProtocol(reader)
await loop.connect_read_pipe(lambda: protocol, sys.stdin)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ async def amain():
# Ask for a config value and print it in the viewer logs
await viewer_control_api.get("Global", "StatsPilotFile")
loop = asyncio.new_event_loop()
loop.run_until_complete(amain())
asyncio.run(amain())
"""


Expand Down

0 comments on commit 0778442

Please sign in to comment.