-
Notifications
You must be signed in to change notification settings - Fork 626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a convenience API for dispatching blocking work #1563
Comments
Definite yes from me! We should have one for [I do like the name exactly as proposed, only thing I'd change is to put a |
I've written code that's almost exactly like this in a couple of projects, with one exception: I built the function on top of an extension on extension DispatchQueue {
func asyncWithFuture<ReturnValue>(
on loop: EventLoop,
_ block: () throws -> ReturnValue
) -> EventLoopFuture<ReturnValue>
} Then the composition is very straightforward, and also allows straightforward compositions of the extension EventLoopFuture {
func flatMap<NewValue>(
onto queue: DispatchQueue,
_ block: (Value) throws -> NewValue
) -> EventLoopFuture<NewValue> {
return self.flatMap { result in
queue.asyncWithFuture { block(result) }
}
}
} This allows the widest flexibility in composition: if you're Dispatch-first, you have a simple construct like Just my 2¢. |
+1 from me too. I'm okay dropping the Should we add |
I think we should have a word like |
+1 for keeping "blocking" in the name, such things need careful consideration and spelling it out is more important than short method names IMO. No strong feels about extension being on DispatchQueue or ELF though hm |
I think we should do both tbh. The ELF |
True, good point. |
Just to clarify, the issue here is that you can't invoke a blocking callback from |
Correct, we don't want to do any blocking work on the event loop. |
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(on loop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit test for this function. Extention to EventLoopFuture with the following functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) -> Void) -> EventLoopFuture<NewValue> - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allow scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(on loop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit test for this function. Extention to EventLoopFuture with the following functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allow scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(on loop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit test for this function. Extention to EventLoopFuture with the following functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allow scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(on loop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit test for this function. Extention to EventLoopFuture with the following functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allow scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(on loop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit test for this function. Extention to EventLoopFuture with the following functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(on loop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit test for this function. Extention to EventLoopFuture with the following functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escaping (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(on loop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit test for this function. Extention to EventLoopFuture with the following functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escaping (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(eventLoop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit tests for this function both when the promise succeeds and fails. Extention to EventLoopFuture with the following public functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escaping (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks. Fixed George feedback
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(eventLoop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit tests for this function both when the promise succeeds and fails. Extention to EventLoopFuture with the following public functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escaping (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(eventLoop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit tests for this function both when the promise succeeds and fails. Extention to EventLoopFuture with the following public functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escaping (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairl common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(eventLoop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit tests for this function both when the promise succeeds and fails. Extention to EventLoopFuture with the following public functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escaping (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairly common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(eventLoop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit tests for this function both when the promise succeeds and fails. Extention to EventLoopFuture with the following public functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escaping (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks.
Motivation: SwiftNIO lacks a convenience API for performing blocking IO / tasks. As this is a fairly common task it then requires the clients to make ad hoc implementations that address this requirement. Modifications: Extension to DispatchQueue with the following method to schedule a work item to the `DispatchQueue` and return and `EventLoopFuture` for the result returned: - `asyncWithFuture<NewValue>(eventLoop: EventLoop, _ callbackMayBlock: @escaping () throws -> NewValue) -> EventLoopFuture<NewValue>` Added new unit tests for this function both when the promise succeeds and fails. Extention to EventLoopFuture with the following public functions: - `flatMapBlocking<NewValue)(onto queue DispatchQueue, _ callbackMayBlock: @escpaing (Value) throws -> NewValue) -> EventLoopFuture<NewValue>` - `whenSuccessBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Value) -> Void) -> EventLoopFuture<NewValue>` - `whenFailureBlocking()onto queue DispatchQueue, _ callbackMayBlock: @escaping (Error) -> Void) -> EventLoopFuture<NewValue>` - `whenCompleteBlocking(onto queue DispatchQueue, _ callbackMayBlock: @escaping (Result<Value, Error>) -> Void) -> EventLoopFuture<NewValue>` These functions may all be called safely with callbacks that perform blocking IO / Tasks. Added new unit tests to EventLoopFutureTest.swift for each new function. Result: New public API for `EventLoopFuture` that allows scheduling of blocking IO / Tasks.
In order to perform blocking work, you current have to either use a DispatchQueue with a promise or setup your own NIOThreadPool. It would be great if NIO can provide a convenience API for scheduling blocking work that returns a future. This could be something like:
The text was updated successfully, but these errors were encountered: