Coverage for /usr/lib/python3/dist-packages/gpiozero/threads.py: 62%
32 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-05 16:40 +0100
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-05 16:40 +0100
1# vim: set fileencoding=utf-8:
2#
3# GPIO Zero: a library for controlling the Raspberry Pi's GPIO pins
4#
5# Copyright (c) 2016-2021 Dave Jones <dave@waveform.org.uk>
6# Copyright (c) 2016 Andrew Scheller <github@loowis.durge.org>
7#
8# SPDX-License-Identifier: BSD-3-Clause
10from __future__ import (
11 unicode_literals,
12 print_function,
13 absolute_import,
14 division,
15 )
16str = type('')
18from threading import Thread, Event
20from .exc import ZombieThread
23_THREADS = set()
24def _threads_shutdown():
25 while _THREADS:
26 threads = _THREADS.copy()
27 # Optimization: instead of calling stop() which implicitly calls
28 # join(), set all the stopping events simultaneously, *then* join
29 # threads with a reasonable timeout
30 for t in threads:
31 t.stopping.set()
32 for t in threads:
33 t.join(10)
36class GPIOThread(Thread):
37 def __init__(self, target, args=(), kwargs=None, name=None):
38 if kwargs is None: 38 ↛ 40line 38 didn't jump to line 40, because the condition on line 38 was never false
39 kwargs = {}
40 self.stopping = Event()
41 super(GPIOThread, self).__init__(None, target, name, args, kwargs)
42 self.daemon = True
44 def start(self):
45 self.stopping.clear()
46 _THREADS.add(self)
47 super(GPIOThread, self).start()
49 def stop(self, timeout=10):
50 self.stopping.set()
51 self.join(timeout)
53 def join(self, timeout=None):
54 super(GPIOThread, self).join(timeout)
55 if self.is_alive(): 55 ↛ 56line 55 didn't jump to line 56, because the condition on line 55 was never true
56 assert timeout is not None
57 # timeout can't be None here because if it was, then join()
58 # wouldn't return until the thread was dead
59 raise ZombieThread(
60 "Thread failed to die within %d seconds" % timeout)
61 else:
62 _THREADS.discard(self)