| 1 |
# Copyright (c) 2018, Science and Technology Facilities Council |
| 2 |
# This software is distributed under a BSD licence. See LICENSE.txt. |
| 3 |
|
| 4 |
""" |
| 5 |
future_mrcfile |
| 6 |
-------------- |
| 7 |
|
| 8 |
Module which exports the :class:`FutureMrcFile` class. |
| 9 |
|
| 10 |
Classes: |
| 11 |
:class:`FutureMrcFile`: An object which represents an MRC file being |
| 12 |
opened asynchronously. |
| 13 |
|
| 14 |
""" |
| 15 |
|
| 16 |
# Import Python 3 features for future-proofing |
| 17 |
from __future__ import (absolute_import, division, print_function, |
| 18 |
unicode_literals) |
| 19 |
|
| 20 |
import threading |
| 21 |
|
| 22 |
|
| 23 |
class FutureMrcFile(object): |
| 24 |
"""Object representing an MRC file being opened asynchronously. |
| 25 |
|
| 26 |
This API deliberately mimics a :class:`~concurrent.futures.Future` object |
| 27 |
from the :mod:`concurrent.futures` module in Python 3.2+ (which we do |
| 28 |
not use directly because this code still needs to run in Python 2.7). |
| 29 |
|
| 30 |
""" |
| 31 |
|
| 32 |
def __init__(self, open_function, args=(), kwargs={}): |
| 33 |
"""Initialise a new :class:`FutureMrcFile` object. |
| 34 |
|
| 35 |
This constructor starts a new thread which will invoke the callable |
| 36 |
given in ``open_function`` with the given arguments. |
| 37 |
|
| 38 |
Args: |
| 39 |
open_function: The callable to use to open the MRC file. (This will |
| 40 |
normally be :func:`mrcfile.open`, but could also be |
| 41 |
:class:`~mrcfile.mrcfile.MrcFile` or any of its subclasses.) |
| 42 |
args: A tuple of positional arguments to use when ``open_function`` |
| 43 |
is called. (Normally a 1-tuple containing the name of the file |
| 44 |
to open.) |
| 45 |
kwargs: A dictionary of keyword arguments to use when |
| 46 |
``open_function`` is called. |
| 47 |
""" |
| 48 |
self._result_holder = [None] |
| 49 |
self._open_function = open_function |
| 50 |
self._thread = threading.Thread(target=self._run, |
| 51 |
args=args, |
| 52 |
kwargs=kwargs) |
| 53 |
self._thread.start() |
| 54 |
|
| 55 |
def _run(self, *args, **kwargs): |
| 56 |
"""Call the open function and store the result in the holder list. |
| 57 |
|
| 58 |
(For internal use only.) |
| 59 |
""" |
| 60 |
try: |
| 61 |
mrc = self._open_function(*args, **kwargs) |
| 62 |
self._result_holder[0] = mrc |
| 63 |
except Exception as ex: |
| 64 |
self._result_holder[0] = ex |
| 65 |
|
| 66 |
def cancel(self): |
| 67 |
"""Return :data:`False`. |
| 68 |
|
| 69 |
(See :meth:`concurrent.futures.Future.cancel` for more details. This |
| 70 |
implementation does not allow jobs to be cancelled.) |
| 71 |
""" |
| 72 |
return False |
| 73 |
|
| 74 |
def cancelled(self): |
| 75 |
"""Return :data:`False`. |
| 76 |
|
| 77 |
(See :meth:`concurrent.futures.Future.cancelled` for more details. |
| 78 |
This implementation does not allow jobs to be cancelled.) |
| 79 |
""" |
| 80 |
return False |
| 81 |
|
| 82 |
def running(self): |
| 83 |
"""Return :data:`True` if the :class:`~mrcfile.mrcfile.MrcFile` is |
| 84 |
currently being opened. |
| 85 |
|
| 86 |
(See :meth:`concurrent.futures.Future.running` for more details.) |
| 87 |
""" |
| 88 |
return self._thread.is_alive() |
| 89 |
|
| 90 |
def done(self): |
| 91 |
"""Return :data:`True` if the file opening has finished. |
| 92 |
|
| 93 |
(See :meth:`concurrent.futures.Future.done` for more details.) |
| 94 |
""" |
| 95 |
return not self.running() |
| 96 |
|
| 97 |
def result(self, timeout=None): |
| 98 |
"""Return the :class:`~mrcfile.mrcfile.MrcFile` that has been opened. |
| 99 |
|
| 100 |
(See :meth:`concurrent.futures.Future.result` for more details.) |
| 101 |
|
| 102 |
Args: |
| 103 |
timeout: Time to wait (in seconds) for the file opening to finish. |
| 104 |
If ``timeout`` is not specified or is :data:`None`, there is no |
| 105 |
limit to the wait time. |
| 106 |
|
| 107 |
Returns: |
| 108 |
An :class:`~mrcfile.mrcfile.MrcFile` object (or one of its |
| 109 |
subclasses). |
| 110 |
|
| 111 |
Raises: |
| 112 |
:exc:`RuntimeError`: If the operation has not finished within the |
| 113 |
time limit set by ``timeout``. (Note that the type of this |
| 114 |
exception will change in future if this class is replaced by |
| 115 |
:class:`concurrent.futures.Future`.) |
| 116 |
:exc:`Exception`: Any exception raised by the |
| 117 |
:class:`~mrcfile.mrcfile.MrcFile` opening operation will be |
| 118 |
re-raised here. |
| 119 |
""" |
| 120 |
result = self._get_result(timeout) |
| 121 |
if isinstance(result, Exception): |
| 122 |
raise result |
| 123 |
else: |
| 124 |
return result |
| 125 |
|
| 126 |
def exception(self, timeout=None): |
| 127 |
"""Return the exception raised by the file opening operation. |
| 128 |
|
| 129 |
(See :meth:`concurrent.futures.Future.exception` for more details.) |
| 130 |
|
| 131 |
Args: |
| 132 |
timeout: Time to wait (in seconds) for the operation to finish. If |
| 133 |
``timeout`` is not specified or is :data:`None`, there is no |
| 134 |
limit to the wait time. |
| 135 |
|
| 136 |
Returns: |
| 137 |
An :exc:`Exception`, if one was raised by the file opening |
| 138 |
operation, or :data:`None` if no exception was raised. |
| 139 |
|
| 140 |
Raises: |
| 141 |
:exc:`RuntimeError`: If the operation has not finished within the |
| 142 |
time limit set by ``timeout``. (Note that the type of this |
| 143 |
exception will change in future if this class is replaced by |
| 144 |
:class:`concurrent.futures.Future`.) |
| 145 |
""" |
| 146 |
result = self._get_result(timeout) |
| 147 |
if isinstance(result, Exception): |
| 148 |
return result |
| 149 |
else: |
| 150 |
return None |
| 151 |
|
| 152 |
def _get_result(self, timeout): |
| 153 |
"""Return the result or exception from the file opening operation. |
| 154 |
|
| 155 |
(For internal use only.) |
| 156 |
""" |
| 157 |
self._thread.join(timeout=timeout) |
| 158 |
if self._thread.is_alive(): |
| 159 |
raise RuntimeError('Timed out waiting for result') |
| 160 |
return self._result_holder[0] |
| 161 |
|
| 162 |
def add_done_callback(self, fn): |
| 163 |
"""Not implemented. |
| 164 |
|
| 165 |
(See :meth:`concurrent.futures.Future.add_done_callback` for more details.) |
| 166 |
""" |
| 167 |
raise NotImplementedError |