PyBitmessage-2021-04-27/src/helper_threading.py

34 lines
690 B
Python
Raw Normal View History

2018-04-07 09:44:43 +00:00
"""Helper threading perform all the threading operations."""
from contextlib import contextmanager
import threading
2019-08-18 04:24:26 +00:00
def set_thread_name(name):
"""Set the thread name for external use (visible from the OS)."""
threading.current_thread().name = name
2018-04-07 09:44:43 +00:00
class StoppableThread(object):
def initStop(self):
self.stop = threading.Event()
self._stopped = False
2018-04-07 09:44:43 +00:00
def stopThread(self):
self._stopped = True
self.stop.set()
2018-04-07 09:44:43 +00:00
class BusyError(threading.ThreadError):
pass
@contextmanager
def nonBlocking(lock):
locked = lock.acquire(False)
if not locked:
raise BusyError
try:
yield
finally:
lock.release()