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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async finalizers to the request-context #59

Closed
gilbsgilbs opened this issue Jun 26, 2017 · 3 comments
Closed

Add async finalizers to the request-context #59

gilbsgilbs opened this issue Jun 26, 2017 · 3 comments

Comments

@gilbsgilbs
Copy link

gilbsgilbs commented Jun 26, 2017

Hi,
My project is Python 3.5 only, and it's therefore impossible for me to use async generators in fixtures. But we might be able to workaround this if it were possible to define async finalizers to request.

E.g.

@pytest.fixture
async def foo_fixture(request):
    async def fin():
        await some_stuff()
    request.addasyncfinalizer(fin)
    return 42

Then it would await for each async finalizers before tear down.

Not sure whether it would involve monkey patching on the request object, or what would be acceptable. Maybe there are also more proper solutions that doesn't involve request or that make use of already existing features? I already tried to use loop.create_task() in a standard finalizer, unfortunately, the created task seem to execute after teardown which produces the following error message:
2017-06-26 18:15:50,262 - asyncio - ERROR - Task was destroyed but it is pending!

Many thanks!

@gilbsgilbs gilbsgilbs changed the title Await async finalizer to request Add async finalizer to the request-context Jun 26, 2017
@gilbsgilbs gilbsgilbs changed the title Add async finalizer to the request-context Add async finalizers to the request-context Jun 26, 2017
@gilbsgilbs
Copy link
Author

Unlike what I thought, it appears that the event loop isn't running inside finalizers. That means that my task would indeed never have been executed.

Consequently, this achieves what I was looking for:

@pytest.fixture
def foo_fixture(request, event_loop):
    def fin():
        async def afin():
            await some_stuff()
        event_loop.run_until_complete(afin())    
    request.addfinalizer(fin)
    return 42

I let you choose to close this issue or not. My point of view is that it would at least deserve a proper documentation. Better API would be nice to have, but since it's py35 only use-case, I don't really see the point, finally.

I can submit a documentation PR for this if you will. Let me know.

@dimaqq
Copy link

dimaqq commented Jul 4, 2017

My 2c: just document it.

IMO 3.6 asyncio is way better than 3.5 and we (as a community) should steer new developers to 3.6.

@gilbsgilbs
Copy link
Author

@dimaqq Thanks! Can't agree more. The point is that LTS versions of some well-known Linux distros won't get support for py36 unless using third-party packages. So I'll just stick to 3.5 until next LTS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants