1 2 3 4 5 6 7 8 9 10 11 12 13 | auto socket = new TAsyncSocket(someTAsyncSocketManager(), host, port); // … socket.asyncManager.execute(socket, { SomeThriftStruct s; // Waiting for socket I/O will not block an entire thread but cause // the async manager to execute another task in the meantime, because // we are using TAsyncSocket instead of TSocket. s.read(socket); // Do something with s, e.g. set a TPromise result to it. writeln(s); }); |
Parameters:
transport | The TAsyncTransport the work delegate will operate on. Must be associated with this TAsyncManager instance. |
work | The operations to execute on the given transport. Must never throw, errors should be handled in another way. nothrow semantics are difficult to enforce in combination with fibres though, so currently exceptions are just swallowed by TAsyncManager implementations. |
cancellation | If set, can be used to request cancellatinon of this work item if it is still waiting to be executed. |
Note:
Parameters:
duration | The amount of time to wait before starting to execute the work delegate. |
work | The code to execute after the specified amount of time has passed. |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // A very basic example – usually, the actuall work item would enqueue // some async transport operation. auto asyncMangager = someAsyncManager(); TFuture!int calculate() { // Create a promise and asynchronously set its value after three // seconds have passed. auto promise = new TPromise!int; asyncManager.delay(dur!"seconds"(3), { promise.succeed(42); }); // Immediately return it to the caller. return promise; } // This will wait until the result is available and then print it. writeln(calculate().waitGet()); |
Parameters:
waitFinishTimeout | If positive, waits for all work items to be finished for the specified amount of time, if negative, waits for completion without ever timing out, if zero, immediately shuts down the background facilities. |
Parameters:
socket | The socket to listen for events at. |
eventType | The type of the event to listen for. |
timeout | The period of time after which the listener will be called with TAsyncEventReason.TIMED_OUT if no event happened. |
listener | The delegate to call when an event happened. |