Skip to content

Commit

Permalink
Reduce CPU usage and power consumption by a lot
Browse files Browse the repository at this point in the history
  • Loading branch information
N1tR0 committed Jul 3, 2022
1 parent b6f6f8a commit 34c3f84
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions gw2rpc/gw2rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def fetch_support_invite():
self.mumble_objects = self.create_mumble_objects()
self.timeticks = 0
self.prev_char = None
# Interval to sleep for the while true loop. Will increase to 5 if game is not running to reduce CPU usage
self.interval = 1 / 2
# Select the first mumble object as initially in focus
if len(self.mumble_objects) > 0:
self.game = self.mumble_objects[0][0]
Expand Down Expand Up @@ -435,7 +437,7 @@ def update_mumble_links():
self.last_map_info = map_info
# 800 ticks are approx 20 minutes (not accurate!). Time it takes to update Guild in GW2 API.
# Query again after this time interval, else keep the previously known guild
if (not self.prev_char) or ((self.prev_char and data["name"] != self.prev_char.name)) or (self.timeticks % 800 == 0):
if (not self.prev_char) or ((self.prev_char and data["name"] != self.prev_char.name)) or (self.timeticks == 0):
# Query GW2API on character swap or every 20 minutes for guild info
character = Character(data)
# Keep the guild tag from the old character
Expand Down Expand Up @@ -713,6 +715,7 @@ def check_for_running_rpc():
while True:
try:
update_gw2_process()
self.interval = 1 / 2
if self.game and not self.game.memfile:
self.game.create_map()
try:
Expand All @@ -729,18 +732,17 @@ def check_for_running_rpc():
if self.sdk.app:
self.sdk.set_activity(data)
self.sdk.app.run_callbacks()

except BrokenPipeError:
raise GameNotRunningError # To start a new connection
self.timeticks = (self.timeticks + 1) % 1000
except GameNotRunningError:
self.interval = 5
if self.game:
self.game.close_map()
if self.sdk.app:
self.sdk.activity_manager.clear_activity(self.sdk.callback)
self.sdk.close()
log.debug("Killing SDK")
time.sleep(1/10)
self.timeticks = (self.timeticks + 1) % 800
time.sleep(self.interval)
except Exception as e:
log.critical("GW2RPC has crashed", exc_info=e)
create_msgbox(
Expand Down

0 comments on commit 34c3f84

Please sign in to comment.