emmental/lib/thread.py

36 lines
818 B
Python

# Copyright 2021 (c) Anna Schumaker.
from . import publisher
import threading
Start = publisher.Publisher()
class Thread:
def __init__(self, func):
self.func = func
self.thread = None
self.lock = threading.Lock()
def __call__(self):
with self.lock:
if self.thread:
return None
self.thread = threading.Thread(target = self.__func__)
self.thread.start()
Start.publish(self)
return self
def __func__(self):
self.func()
with self.lock:
self.thread = None
def join(self):
if self.thread:
self.thread.join()
def running(self):
with self.lock:
if self.thread:
return self.thread.is_alive()
return False