Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider delegating to nest_asyncio #8650

Open
t0yv0 opened this issue Dec 29, 2021 · 0 comments
Open

Consider delegating to nest_asyncio #8650

t0yv0 opened this issue Dec 29, 2021 · 0 comments
Labels
kind/enhancement Improvements or new features language/python

Comments

@t0yv0
Copy link
Member

t0yv0 commented Dec 29, 2021

Hello!

  • Vote on this issue by adding a 馃憤 reaction
  • If you want to implement this feature, comment to let us know (we'll work with you on design, scheduling, etc.)

Issue details

Pulumi Python SDK carries logic in sync_await.py that is non-trivial and seems to overlap with the https://github.com/erdewit/nest_asyncio library. If this makes sense for the project, it could be nice to re-use and delegate to nest_asyncio.

Illustration:

import asyncio

import nest_asyncio
nest_asyncio.apply()


def do_sync(another_task):
    # it is not normally possible to await an aio task inside a def
    # that is not yet async, like this one, when this def is invoked
    # from a task itself. What one gets in standard Python is:
    #
    # raise RuntimeError('This event loop is already running')
    #
    # Pulumi introduecs _sync_await to fix this.
    #
    # The alternative is to enable `nest_asyncio` that makes
    # `run_until_complete` work as expected.
    loop = asyncio.get_event_loop()
    return loop.run_until_complete(another_task)


async def do_io(another_task):
    print('io start')
    await asyncio.sleep(5)
    result = do_sync(another_task)
    print(f'result = {result}')
    print('io end')


async def do_other_things():
    print('doing other things')
    return 'other things'


loop = asyncio.get_event_loop()

f1 = loop.create_task(do_other_things())
f2 = loop.create_task(do_io(f1))

loop.run_until_complete(f1)
loop.run_until_complete(f2)

loop.close()

Affected area/feature

Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements or new features language/python
Projects
None yet
Development

No branches or pull requests

1 participant