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_timeout decorator #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

mosquito
Copy link

@mosquito mosquito commented Jun 3, 2017

New async_timeout decorator.

class TimedOutTestCase(TestCase):
    @async_timeout
    async def test_default_timeout(self):
        await asyncio.sleep(999, loop=self.loop)

    @async_timeout(seconds=1)
    async def test_custom_timeout(self):
        await asyncio.sleep(999, loop=self.loop)

@mosquito mosquito changed the title [fea] add async_timeout decorator Add async_timeout decorator Jun 3, 2017
@Martiusweb
Copy link
Owner

Hi,

Thanks for you contribution. Can you explain why you want to introduce this feature and in which case it is useful for you?

Thanks

@mosquito
Copy link
Author

mosquito commented Jun 5, 2017

Often some tests got stuck. For example future was created without loop= parameter or other dummy errors. In this case you have to wait CI timeout or cancel jobs manually.

@Martiusweb
Copy link
Owner

It can be useful to have a timeout set on each test, but I feel that your proposal of a decorator targets a use case too broad to be included in asynctest as it is: this should be in a "utils" library (or maybe even in asyncio itself).

What do you think?

@mosquito
Copy link
Author

mosquito commented Jun 5, 2017

Hmm... In my cases it's so useful for tests. But you are right, that's a common implementation. Actually I was inspired by gen_test decorator from tornado.

@Kentzo
Copy link
Contributor

Kentzo commented Nov 20, 2017

With the async_timeout package decorator could be implemented like this:

...
    DEFAULT_ASYNC_TIMEOUT = 1.0

    @classmethod
    def with_timeout(cls, timeout=None):
        if timeout is None:
            timeout = cls.DEFAULT_ASYNC_TIMEOUT

        def decorator(coro):
            @functools.wraps(coro)
            async def wrapper(*args, **kwargs):
                with async_timeout.timeout(timeout):
                    await coro(*args, **kwargs)

            return wrapper

        return decorator
...

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

Successfully merging this pull request may close these issues.

3 participants