Skip to content

Commit

Permalink
add graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed Oct 31, 2024
1 parent 0677f64 commit b8ff532
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,24 @@ async def game_loop(self):
await asyncio.sleep(1)

async def main():

client = BatMudClient()
try:
await client.game_loop()
except KeyboardInterrupt:
print("\nExiting...")
print("\nGracefully shutting down...")
except Exception as e:
print(f"\nError: {e}")
finally:
if client.telnet:
reader, writer = client.telnet
writer.close() # Just close the writer
await asyncio.sleep(0.1) # Small delay to allow the connection to close gracefully
writer.close()
print("Connection closed.")

if __name__ == "__main__":
asyncio.run(main())
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nExiting...")
except Exception as e:
print(f"\nFatal error: {e}")
sys.exit(0)

0 comments on commit b8ff532

Please sign in to comment.