python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

CPU 100% busy on Raspberry

Open vagfed opened this issue 4 years ago • 11 comments

I have just started to use sinricpro python library on a Raspberry pizero and CPU is constantly at 100% with processor quite hot. Probably there is an event loop very tight. Is there a way to reduce CPU load? I basically use three lines of code:

client = SinricPro(appKey, deviceIdArr, callbacks, event_callbacks=None, enable_log=False, restore_states=True, secretKey=secretKey)
udp_client = SinricProUdp(callbacks,deviceIdArr,enable_trace=False)
client.handle_all(udp_client)

vagfed avatar Jul 14 '20 22:07 vagfed

It's happening because one thread is completely dedicated to udp actions (offline).

thegoliathgeek avatar Jul 15 '20 05:07 thegoliathgeek

may be commented it for now?

kakopappa avatar Jul 16 '20 05:07 kakopappa

If I comment the udp_client = SinricProUdp(callbacks,deviceIdArr,enable_trace=False) line I can no longer call client.handle_all(udp_client). How should I use the SinricPro object to make it handle Alexa data? The handle_all function uses udp_client in both threads and I think the t1 is the one that handled Alexa data,

def handle_all(self, udp_client):
        try:
            t1 = Thread(target=self.handle_clients, args=(self.socket.handle, udp_client))
            t2 = Thread(target=udp_client.listen)

vagfed avatar Jul 16 '20 07:07 vagfed

I'll update this and make udp optional.

thegoliathgeek avatar Jul 16 '20 17:07 thegoliathgeek

Hello @vagfed . I just updated python-sdk and made udp optional. You can try again with latest version.

thegoliathgeek avatar Jul 27 '20 19:07 thegoliathgeek

I have the same problem. My main function is now: client = SinricPro(appKey, deviceIdArr, callbacks, enable_log=False,restore_states=True,secretKey=secretKey) client.handle_all(None)

marcoaltomonte avatar Dec 07 '20 11:12 marcoaltomonte

In _sinricprosocket.py in function async def handle the first while True is a busy loop.

marcoaltomonte avatar Dec 08 '20 08:12 marcoaltomonte

Fixed by adding:

  1. from time import sleep
  2. sleep(1) between "while True" and "while queue.qsize() > 0:" in handle function in _sinricprosocket.py

marcoaltomonte avatar Dec 08 '20 08:12 marcoaltomonte

hi @marcoaltomonte. Made the sleep customizable to avoid this issue.

client.handle_all(udp_client, sleep=1)

Please check out this in new update

thegoliathgeek avatar Jan 16 '21 07:01 thegoliathgeek

Are these changes part of version 2.4.2 or 2.4.1? I still have 100% cpu usage with this code: client = SinricPro(appKey, deviceIdArr, callbacks, event_callbacks=event_callback, enable_log=False,restore_states=True,secretKey=secretKey) udp_client = SinricProUdp(callbacks,deviceIdArr,enable_trace=False) client.handle_all(udp_client,sleep=1)

I am using version 2.4.1 that was installed with: pip3 install sinricpro

djschwab avatar Nov 10 '21 15:11 djschwab

I found that for me the only way to reduce cpu usage was to add a delay in the Events() procedure. I also found that on my Linux machine my program would become unresponsive after 15 minutes or so if I didn't also include the call to client.event_handler.raiseEvent in the procedure.

def Events(): while True: # Select as per your requirements # REMOVE THE COMMENTS TO USE time.sleep(300) # needed to keep cpu time < 100%
# if this call is not included, device code becomes non-responsive after 15 minutes client.event_handler.raiseEvent(device1, 'setPowerState',data={'state': 'On'}) pass

djschwab avatar Nov 19 '21 01:11 djschwab