I'm new to both Python and multi-threading. I read that a Python thread finishes when the function it is running 'returns'. Should I be worried that my program is spinning up an infinite amount of threads?
I'm asking because sometimes the HTTP request times out and it throws an (expected) error:
Exception in Thread-44
The error is not my worry, but rather the thread number is. Does this means there are indeed 44 threads or that this is simply the 44th thread (but the previous 43 or so closed gracefully)?
Code:
def start(self, error_refresh=None):
self.timeout_id = GLib.timeout_add_seconds(10, self.check_price)
def stop(self):
if self.timeout_id not 0:
GLib.source_remove(self.timeout_id)
def check_price(self):
self.async_get(URL, callback=self._parse_result)
return True
def async_get(self, *args, callback=None, timeout=15, **kwargs):
if callback:
def callback_with_args(response, *args, **kwargs):
callback(response)
kwargs['hooks'] = {'response': callback_with_args}
kwargs['timeout'] = timeout
thread = Thread(target=requests.get, args=args, kwargs=kwargs)
thread.start()
def _parse_result(self, data):
## code for parsing here
GLib.idle_add(self.indicator.set_data,label, bid, high, low, ask, vol)