Replace destructor with context manager (#223)

* Make TorchNeuralNet a context manager.

Add __enter__/__exit__ method to kill Torch subprocess

* Update doc for the TorchNeuralNet

* Fix code snippets

* Update doc for enter/exit methods

* Format docstring for enter/exit methods
This commit is contained in:
Alex 2017-01-08 01:37:23 +02:00 committed by Brandon Amos
parent 7659f7907d
commit f9764b9c5d
1 changed files with 29 additions and 4 deletions

View File

@ -32,7 +32,27 @@ os.environ['TERM'] = 'linux'
class TorchNeuralNet:
"""Use a `Torch <http://torch.ch>`_ subprocess for feature extraction."""
"""
Use a `Torch <http://torch.ch>`_ subprocess for feature extraction.
It also can be used as context manager using `with` statement.
.. code:: python
with TorchNeuralNet(model=model) as net:
# code
or
.. code:: python
net = TorchNeuralNet(model=model)
with net:
# use Torch' neuronal network
In this way Torch processes will be closed at the end of the `with` block.
`PEP 343 <https://www.python.org/dev/peps/pep-0343/>`_
"""
#: The default Torch model to use.
defaultModel = os.path.join(myDir, '..', 'models', 'openface', 'nn4.small2.v1.t7')
@ -68,10 +88,15 @@ class TorchNeuralNet:
self.p.kill()
atexit.register(exitHandler)
def __del__(self):
"""__del__(self)
def __enter__(self):
"""Part of the context manger' protocol. See PEP 343"""
return self
Kill the Lua subprocess.
def __exit__(self, exc_type, exc_value, traceback):
"""
Clean up resources when leaves `with` block.
Kill the Lua subprocess to prevent zombie processes.
"""
if self.p.poll() is None:
self.p.kill()