CharlesBlonde / libsoundtouch

Bose Soundtouch Python library
http://libsoundtouch.readthedocs.io
Other
94 stars 34 forks source link

Device discovery increasing thread count #27

Open chwiede opened 6 years ago

chwiede commented 6 years ago

Hi,

thanks for implementing this library, running great except one little thing.

I'm using the lib on a headless raspberry pi in a long-running process. To detect changes of my soundtouch-devices i'm callnig libsoundtouch.discover_devices frequently (e.g. all 30 minutes). After some while, i'm getting this error:

File "/usr/lib/python2.7/threading.py", line 736, in start 
    _start_new_thread(self.__bootstrap, ())
error: can't start new thread

Testscript

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading
import libsoundtouch

for i in range(10):
    print("starting discovery... ", end='')
    libsoundtouch.discover_devices(timeout=1)
    print("active threads are now %s" % threading.active_count())

Output

starting discovery... active threads are now 4
starting discovery... active threads are now 7
starting discovery... active threads are now 10
starting discovery... active threads are now 13
starting discovery... active threads are now 16
starting discovery... active threads are now 19
starting discovery... active threads are now 22
starting discovery... active threads are now 25
starting discovery... active threads are now 28
starting discovery... active threads are now 31

So it looks like every call leaves 3 open threads. I'd looked into the code but i didn't find anything suspicious...

Any ideas?

Thanks in advance!

chwiede commented 6 years ago

Figured out my problem. ServiceBrowser.cancel() should be called in discover_devices.

Additonally i've added zeroconf as parameter. So it's possible to keep control over this instance. Default it's set to None, and it will be created in method as before.

Another approach would be to clean up the created zeroconf-instance, but this needs some time.

Please have a look, if accepted i'll make a pull request.