Skip to content

Should we have a way to let some other coroutine runner take temporary control of a task? #649

@njsmith

Description

@njsmith

So there's a cute hack, which as far as I know @agronholm was the first to discover, where you can use an async with block to temporarily switch between coroutine runners. For example:

async def somefunc():
    await do_something_in_main_thread()
    async with in_worker_thread():
        do_something_blocking()
    await do_something_in_main_thread()

Another possible application would be to allow trio-asyncio to switch back and forth between trio mode and asyncio mode within a single function.

This is kind of brain melting, but the implementation is surprisingly simple: a task's current execution is represented by a coroutine object. So, you suspend the coroutine, and from Trio's point of view it becomes "blocked"; then you take that coroutine object and give it to another coroutine runner to iterate for a while. Later, it gets handed back and Trio "resumes" it.

You can't actually implement something like this right now though, using Trio's public API. You can suspend a task, and get the coroutine object... but the coroutine is suspended inside wait_task_rescheduled, so to resume you need to send in the kind of value that wait_task_rescheduled is expecting, and that's not part of the public API. Similarly, to resume a task, trio.hazmat.reschedule will send in some kind of value, but you're not supposed to know how to interpret it – only wait_task_rescheduled knows that, so to resume the task you have to somehow get back into wait_task_rescheduled.

But it wouldn't be terribly hard to add a new suspend/resume API just for this. In trio.hazmat, obviously! This is extremely hazmat-y. But that's why trio.hazmat exists, so we can expose hazmat-y things. Something like:

@types.coroutine
def detach_task(abort_fn):
    yield some_magic_value(abort_fn)

@types.coroutine
def reattach_task(yield_value):
    got = yield yield_value
    do_whatever_wait_task_rescheduled_would_do_with(got)

So the idea is that to detach the task from trio, you do await detach_task(abort_fn), which is exactly like wait_task_rescheduled except that it has a documented API for what you send into it to resume the task (maybe None, or maybe it's return yield ..., so whoever resumes the coroutine can choose the return value). And then you iterate the coroutine however you want, in whatever context you want – as a synchronous thread, as an asyncio Task, whatever. And then, when you're finished, you do await reattach_task(yield_value), and it sends that yield_value to the coroutine runner – so for asyncio maybe it's a special Future, whatever you like, nothing to do with trio really, just make sure that your other coroutine runner stops trying to mess with this coroutine objects – and then it goes into a state where you can call trio.hazmat.reschedule and continue.

I think this + #642 would make it possible to fully implement curio's "async threads" API as a trio library.

So... is it a good idea?

I've known about this trick for quite some time, and hesitated because async with in_worker_thread seemed so magical that it was in dubious taste. But... OTOH trio's lowest-level APIs do try hard to make anything possible, regardless of whether I think it's in good taste or not :-). And using this in trio-asyncio seems like it might be compelling? And the API described above is would not be too terrible to implement or maintain. (That's the other thing... I haven't been excited about this because I couldn't figure out how to even do it while respecting trio's abstraction boundaries. But then today I figured out the API above.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions