A story when I was addicted to a project using Python 3 series.
import threading
class ThreadRunner(object):
def __init__(self):
self.__thread = None
self.__event = threading.Event()
def start(self):
self.__event.clear()
self.__thread = threading.Thread(self.__run)
def stop(self):
self.__event.set()
self.__thread.join()
self.__thread = None
def is_running(self):
return not self.__event.is_set()
def __run(self):
raise NotImplementedError("class is abstract.")
It is a very simple utility that combines Thread () and Event () of threading. Have the child class override __run () and The processing part was unimplemented so that it could be used nicely.
import time
from .ThreadRunner import ThreadRunner
class TimeWaiter(ThreadRunner):
def __init__(self):
super().__init__()
def __run(self):
while True:
#Some processing
time.sleep(1)
↑ Inherit like this,
from TimeWaiter import TimeWaiter
waiter = TimeWaiter()
waiter.start()
Now.
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Program Files\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\test\ThreadRunner.py", line 23, in __run
raise NotImplementedError("class is abstract.")
NotImplementedError: class is abstract.
I spit out an error. It should have been overridden and disabled The process of throwing NotImplementedError () of the parent method is called.
When I searched for information from one end, I arrived at the following article.
[Note] About the role of underscore "_" in Python http://qiita.com/ikki8412/items/ab482690170a3eac8c76
About naming Python variables and methods (underscore) https://teratail.com/questions/41277
In the comment section of the first article and the answer of the second article, there was an answer to what I was moss. If you add two underscores to the beginning of the function name or variable name, The name will change automatically inside (it seems to be called mangling), and it will be different.
class ThreadRunner(object):
def __init__(self):
self.__thread = None
self.__event = threading.Event()
def start(self):
self.__event.clear()
self.__thread = threading.Thread(self.__run)
def stop(self):
self.__event.set()
self.__thread.join()
self.__thread = None
def is_running(self):
return not self.__event.is_set()
def __run(self):
raise NotImplementedError("class is abstract.")
The ThreadRunner class mentioned earlier is synonymous with the following.
class TimeWaiter(object):
def __init__(self):
self.__thread = None
self.__event = threading.Event()
def start(self):
self.__event.clear()
self.__thread = threading.Thread(self.ThreadRunner__run)
def stop(self):
self.__event.set()
self.__thread.join()
self.__thread = None
def is_running(self):
return not self.__event.is_set()
def ThreadRunner__run(self):
raise NotImplementedError("class is abstract.")
def TimeWaiter__run(self):
while True:
#Some processing
time.sleep(1)
No matter how much the child class inherits, the name will change on the parent class side. It wasn't supposed to be overridden.
In this case, it seemed better to have one underscore. By making one underscore, mangle ring does not work, It will be treated as an override internally.
For those who are familiar with Python, it's natural ... I've just started touching it, so I was quite addicted to it and melted my time ...
That's all for the memorandum.
Recommended Posts