Skip to content

Add sleep in GamepadThread to avoid overloading the CPU #12

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion XInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,18 @@ def clear_filter(self):


class GamepadThread:
def __init__(self, *event_handlers, auto_start=True):
def __init__(self, *event_handlers, auto_start=True, update_frequency=1000):
for event_handler in event_handlers:
if (event_handler is None or not issubclass(type(event_handler), EventHandler)):
raise TypeError("The event handler must be a subclass of XInput.EventHandler")

if not isinstance(update_frequency, (float, int)):
raise TypeError("Update_frequency must be a number")

if update_frequency <= 0:
raise ValueError("Update_frequency must be greater than 0")

self.__update_frequency = update_frequency

self.handlers = set(event_handlers)

Expand All @@ -636,6 +644,7 @@ def __init__(self, *event_handlers, auto_start=True):

def __tfun(self): # thread function
while(self.running): # polling
time.sleep(1 / self.__update_frequency) # sleep to avoid overloading the CPU
self.lock.acquire()
for new_handler in self.queued_new_handlers:
self.handlers.add(new_handler)
Expand Down