Infinity.AsyncOperation¶
Fields¶
None
Methods¶
class |
|
|
|
|
Details¶
- class Infinity.AsyncOperation¶
Infinity.AsyncOperation
is an opaque data type and should only be accessed via the public API functions.- classmethod new(io, run_func, done_func, *user_data)¶
- Parameters:
io (
Infinity.Io
) – TheInfinity.Io
object used to pass back the result of the operation.run_func (
Infinity.AsyncOperationRunFunc
) – A function to run asynchronously in a worker thread, computing the result of the operation.done_func (
Infinity.AsyncOperationDoneFunc
) – A function to be called in the thread of io once the result is available.user_data (
object
orNone
) – Additional user data to pass to both functions.
- Returns:
A new
Infinity.AsyncOperation
. Free withInfinity.AsyncOperation.free
() to cancel the operation.- Return type:
This function creates a new
Infinity.AsyncOperation
. The function given by run_func will be run asynchronously in a worker thread. Once the function finishes, its result is passed back to the main thread defined by io, and done_func is called with the computed result in the main thread.To actually start the asynchronous operation, call
Infinity.AsyncOperation.start
(). This allows to save the returned value into a structure before starting the operation, avoiding a potential race condition if the asynchronous function finishes quickly.The asynchronous operation can be canceled by calling
Infinity.AsyncOperation.free
() on the returnedInfinity.AsyncOperation
object. If the operation is not cancelled and after done_func has been called, the operation is freed automatically and must not be freed by the caller. The caller must also keep a reference to io while the operation is running. Before dropping your reference to io, make sure to free the asynchronous operation. When the last reference to io is dropped, the operation is freed automatically, since it cannot pass back its result to the main thread anymore.
- free()¶
Frees the given asynchronous operation and cancels it if it is currently running. This should only be called to cancel a running operation, or to free an operation that has not been started. In all other cases, the operation is freed automatically.
- start()¶
-
Starts the operation given in self. The operation must have been created before with
Infinity.AsyncOperation.new
(). If the operation cannot be started, error is set andFalse
is returned. In that case, the operation must not be used anymore since it will be automatically freed.