[swift-evolution] [Concurrency] async/await + actors
adam.kemp at apple.com
Fri Aug 25 11:30:11 CDT 2017
> On Aug 25, 2017, at 1:14 AM, Thomas <tclementdev at free.fr> wrote:
>> On 25 Aug 2017, at 01:15, Adam Kemp <adam.kemp at apple.com <mailto:adam.kemp at apple.com>> wrote:
>> I don’t think await should cause the actor’s queue (or any queue) to be suspended. Actor methods should not block waiting for asynchronous things. That’s how you get deadlocks. If an actor method needs to be async then it should work just like any async method on the main queue: it unblocks the queue and allows other messages to be processed until it gets an answer.
>> You do have to be aware of the fact that things can happen in between an await and the next line of code, but conveniently these places are all marked for you. They all say “await”. :)
> It is correct that suspending the queue allows for deadlocks, but not doing it means you can receive messages while still in the middle of another message. For the same reason you may need FIFO ordering in a class to guarantee coherency, you will want this to work in an asynchronous world as well. Take for example some storage class:
> 1. store(object, key)
> 2. fetch(key)
> If you're doing these operations in order, you want the fetch to return the object you just stored. If the 'store' needs to await something in its implementation and we were to not suspend the queue, the fetch would be processed before the object is actually stored and it would return something unexpected.
Actors can use other means to serialize operations if they need to, for instance by using an internal queue of pending operations. It’s better for actors that need this kind of serialization to handle it explicitly than for every actor to suffer from potential deadlocks when doing seemingly straightforward things.
async/await in general is not meant to block anything. It’s explicitly meant to avoid blocking things. That’s what the feature is for. It would be confusing if await did something different for actor methods than it did for every other context.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution