SyncConsumer isn't async - it runs inside a separate synchronous thread. You'll need to get the event loop from the other thread and use call_soon_threadsafe instead!
Andrew On Wed, Aug 14, 2019 at 6:59 PM Dan Merillat <dan.meril...@gmail.com> wrote: > > I have an application that's 99% using the ORM and database work so it is > using SyncConsumer. > However, I have one minor part that needs to use a timer, and I cannot get > it to work for the life of me. > > In an AsyncConsumer, I can use asyncio.ensure_future(self.coroutine()) to > start a timer. > > In sync consumer, nothing I've tried works: > > class TestConsumer(SyncConsumer): > > async def timer_task(self): > print(f'timer task: {self.counter}') > asyncio.sleep(1) > self.counter += 1 > print(f'timer expired: {self.counter}') > > def connect(self): > self.counter = 0 > > self.timer = asyncio.ensure_future(self.timer_task()) > # ERROR:daphne.server:Exception inside application: There is no > current event loop in thread 'ThreadPoolExecutor-0_20'. > > self.timer = > async_to_sync(asyncio.ensure_future)(self.timer_task()) > # ERROR:daphne.server:Exception inside application: You cannot use > AsyncToSync in the same thread as an async event loop - just await the > async function directly. > > I thought SyncConsumer was async under the hood, but I have no idea how to > leverage that. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/6fe7ad95-1bdb-4de2-9ed2-8c566ba27939%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/6fe7ad95-1bdb-4de2-9ed2-8c566ba27939%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFwN1uoGn%2B7C0hLFat%3DjMVgOKL2nYHAuae1i6mrC-JRW6W0uww%40mail.gmail.com.