5.2.4. vlcp.event.future¶
Note
This document is generated from the source file.
Created on 2016/9/28
author: | hubo |
---|
Future is a helper class to simplify the process of retrieving a result from other routines. The implementation is straight-forward: first check the return value, if not set, wait for the event. Multiple routines can wait for the same Future object.
The interface is similar to asyncio, but:
- Cancel is not supported - you should terminate the sender routine instead. But RoutineFuture supports close() (and cancel() which is the same)
- Callback is not supported - start a subroutine to wait for the result instead.
- result() returns None if the result is not ready; exception() is not supported.
- New wait() generator function: get the result, or wait for the result until available. It is always the recommended way to use a future; result() is not recommended.
- ensure_result() which returns a context manager: this should be used in the sender routine, to ensure that a result is always set after exit the with scope. If the result is not set, it is set to None; if an exception is raised, it is set with set_exception.
-
class
vlcp.event.future.
Future
(scheduler)¶ Basic future object
-
__init__
(scheduler)¶ Initialize self. See help(type(self)) for accurate signature.
-
done
()¶ Returns: True if the result is available; False otherwise.
-
ensure_result
(supress_exception=False, defaultresult=None)¶ Context manager to ensure returning the result
-
result
()¶ Returns: None if the result is not ready, the result from set_result, or raise the exception from set_exception. If the result can be None, it is not possible to tell if the result is available; use done() to determine that.
-
set_exception
(exception)¶ Set an exception to Future object, wake up all the waiters
Parameters: exception – exception to set
-
set_result
(result)¶ Set the result to Future object, wake up all the waiters
Parameters: result – result to set
-
wait
(container)¶ Parameters: container – container of current routine Returns: The result, or raise the exception from set_exception. The result is returned to container.retvalue.
-
-
exception
vlcp.event.future.
FutureCancelledException
¶
-
class
vlcp.event.future.
FutureEvent
(*args, **kwargs)¶
-
class
vlcp.event.future.
RoutineFuture
(subprocess, container)¶ Quick wrapper to create a subroutine and return the result to a Future object
-
__init__
(subprocess, container)¶ Start the subprocess
Parameters: - subprocess – a generator process, which returns the result to container.retvalue on exit
- container – the routine container to run the subprocess with
-
cancel
()¶ Same as close()
-
close
()¶ Terminate the subprocess
-