Executes the function in the background. AsynchronousRequest.done is False as long as it is busy, but the program will not halt in the meantime. AsynchronousRequest.value contains the function's return value once done. AsynchronousRequest.error contains the Excep
(self, function, *args, **kwargs)
| 96 | class AsynchronousRequest(object): |
| 97 | |
| 98 | def __init__(self, function, *args, **kwargs): |
| 99 | """ Executes the function in the background. |
| 100 | AsynchronousRequest.done is False as long as it is busy, but the program will not halt in the meantime. |
| 101 | AsynchronousRequest.value contains the function's return value once done. |
| 102 | AsynchronousRequest.error contains the Exception raised by an erronous function. |
| 103 | For example, this is useful for running live web requests while keeping an animation running. |
| 104 | For good reasons, there is no way to interrupt a background process (i.e. Python thread). |
| 105 | You are responsible for ensuring that the given function doesn't hang. |
| 106 | """ |
| 107 | self._response = None # The return value of the given function. |
| 108 | self._error = None # The exception (if any) raised by the function. |
| 109 | self._time = time.time() |
| 110 | self._function = function |
| 111 | self._thread = threading.Thread(target=self._fetch, args=(function,)+args, kwargs=kwargs) |
| 112 | self._thread.start() |
| 113 | |
| 114 | def _fetch(self, function, *args, **kwargs): |
| 115 | """ Executes the function and sets AsynchronousRequest.response. |