asyncio: no current event loop in thread 'MainThread'

caulagi at gmail.com caulagi at gmail.com
Wed Jun 28 07:46:49 EDT 2017


I am trying to write a test for a function using asyncio. Can I mix using unittest.TestCase and asyncio.test_utils.TestCase? When I do that, the loop in test_utils.TestCase seems to affect the other code I have. This is a simplified example -

$ pip freeze
py==1.4.34
pytest==3.1.2

$ cat test_foo.py
import asyncio
import unittest

from asyncio import test_utils


async def foo():
    return True


class Foo1Test(test_utils.TestCase):

    def setUp(self):
        super().setUp()
        self.loop = self.new_test_loop()
        self.set_event_loop(self.loop)

    def test_foo(self):
        res = self.loop.run_until_complete(foo())
        assert res is True


class Foo2Test(unittest.TestCase):

    def test_foo(self):
        loop = asyncio.get_event_loop()
        res = loop.run_until_complete(foo())
        assert res is True


class Foo3Test(test_utils.TestCase):

    def setUp(self):
        super().setUp()
        self.loop = self.new_test_loop()
        self.set_event_loop(self.loop)

    def test_foo(self):
        res = self.loop.run_until_complete(foo())
        assert res is True

$ py.test -v
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.6.1, pytest-3.1.2, py-1.4.34, pluggy-0.4.0 -- /private/tmp/foobar/.env/bin/python3
cachedir: .cache
rootdir: /private/tmp/foobar, inifile:
collected 3 items

test_foo.py::Foo1Test::test_foo PASSED
test_foo.py::Foo2Test::test_foo FAILED
test_foo.py::Foo3Test::test_foo PASSED

==================================================================================== FAILURES ====================================================================================
_______________________________________________________________________________ Foo2Test.test_foo ________________________________________________________________________________

self = <test_foo.Foo2Test testMethod=test_foo>

    def test_foo(self):
>       loop = asyncio.get_event_loop()

test_foo.py:26:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py:678: in get_event_loop
    return get_event_loop_policy().get_event_loop()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <asyncio.unix_events._UnixDefaultEventLoopPolicy object at 0x11053ffd0>

    def get_event_loop(self):
        """Get the event loop.

            This may be None or an instance of EventLoop.
            """
        if (self._local._loop is None and
            not self._local._set_called and
            isinstance(threading.current_thread(), threading._MainThread)):
            self.set_event_loop(self.new_event_loop())
        if self._local._loop is None:
            raise RuntimeError('There is no current event loop in thread %r.'
>                              % threading.current_thread().name)
E           RuntimeError: There is no current event loop in thread 'MainThread'.

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py:584: RuntimeError

What am I missing?



More information about the Python-list mailing list