RuntimeError: got Future <Future pending> attached to a different loop

See original GitHub issue

Description

Getting the following error when running an async flow in orion server or cloud

RuntimeError: Task <Task pending name='anyio.from_thread.BlockingPortal._call_func' coro=<BlockingPortal._call_func() running at /Users/noam.cohen/Library/Caches/pypoetry/virtualenvs/finance-lGLYToYy-py3.9/lib/python3.9/site-packages/anyio/from_thread.py:219> cb=[TaskGroup._spawn.<locals>.task_done() at /Users/noam.cohen/Library/Caches/pypoetry/virtualenvs/finance-lGLYToYy-py3.9/lib/python3.9/site-packages/anyio/_backends/_asyncio.py:726]> got Future <Future pending> attached to a different loop

Reproduction

import asyncio

from dotenv import load_dotenv
from prefect import task, flow

@task
async def print_values(values):
    for value in values:
        await asyncio.sleep(1) # yield
        print(value, end=" ")

@task
def get_numbers():
    return [1, 2]

@flow(name='test-flow')
async def dummy_flow():
    numbers = get_numbers()
    await print_values(numbers)  # runs immediately
    coros = [print_values("abcd"), print_values("6789")]

    # asynchronously gather the tasks
    await asyncio.gather(*coros)



if __name__ == '__main__':
    load_dotenv()
    asyncio.run(dummy_flow())

Environment

prefect 2.08b

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
madkinszcommented, Sep 23, 2022

This does not reproduce on the latest release.

0reactions
anna-gellercommented, Aug 5, 2022

Ahh gotcha, next time if you could link to that discussion, it would help, I canโ€™t remember all that ๐Ÿ˜„

Could you use one of the solutions I suggested, and if that works close the issue here? I understand what you mean, but I think the mechanics here work well enough and not sure if spending more time on optimizing this is worth it as opposed to other really cool things we can do in 2.0 instead

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Using queues results in asyncio exception "got Future ...
run() creates a new loop, and futures created for the queue in one loop can't then be used in the other. Create your...
Read more >
Task got Future <Future pending> attached to a different loop
The error you see is saying that the main() coroutine is running on the event loop started by asyncio.run , but it is...
Read more >
ืขืœ ื”ืฉื’ื™ืื” got Future attached to a different loop ื‘ Python
ืขืœ ื”ืฉื’ื™ืื” got Future attached to a different loop ื‘ Python ... defined at post.py:5> exception=RuntimeError("Task <Task pending name='Task-4'ย ...
Read more >
ASGI Event Loop Gotcha - Rob Blackbourn
ASGI Event Loop Gotcha. I've shed many tears with the following exception: got Future <Future pending> attached to a different loop.
Read more >
tiangolo/fastapi - Gitter
I want to create two models that reference one another: ... My error is always "got Future <Future pending> attached to a different...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found