sinricpro / python-sdk

python3 sdk for sinric pro.
https://sinric.pro
23 stars 9 forks source link

CPU 100% busy on Raspberry #24

Open vagfed opened 4 years ago

vagfed commented 4 years ago

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)
thegoliathgeek commented 4 years ago

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

kakopappa commented 4 years ago

may be commented it for now?

vagfed commented 4 years ago

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)
thegoliathgeek commented 4 years ago

I'll update this and make udp optional.

thegoliathgeek commented 4 years ago

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

marcoaltomonte commented 3 years ago

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 commented 3 years ago

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

marcoaltomonte commented 3 years ago

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

thegoliathgeek commented 3 years ago

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

client.handle_all(udp_client, sleep=1)

Please check out this in new update

djschwab commented 3 years ago

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 commented 2 years ago

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