[python] advice needed: how to run infinite loopS in component to periodicaly do things (or another way?)

431 views
Skip to first unread message

Potens

unread,
Dec 14, 2018, 4:57:48 AM12/14/18
to Autobahn
Hi,

I've a component that should download different things on different delays.

I've tried to create a component that on_join starts different coroutines to run the infinite loop that download, wait the delay and so on.

My problem is, when I quit my app, it spams a lot a "Tasks was never awaited" and, I've tried to await it in on_leave or on_disconnect, but it seems that, when the application quit, those events do not run.

so basically what it does is (simplified with only one loop, but I have 6 of those):

class Downloader:
    def __init__(self, wamp_comp):
         self._wamp = wamp_comp
         self._session = None
         self._wamp.on("join", self._initialize)
         self._wamp.on("join", self._start_dl_loop)
        
    async def _initialize(self, session, details):
        self._session = session

    async def _start_dl_loop(self, session, details):
        await self.get_things(DEFAULT_DELAY)
  
   
    async def get_things(self, delay_sec: float):
        while True:
            try:
                # await aiohttp request
                await asyncio.sleep(delay_sec)


and, when stop:
ERROR:project.endpoints.sync:
2018-12-14T10:45:39
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<Downloader._start_tasks_loop() running at ...> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f7eb6739138>()]> cb=[gather.<locals>._done_callback(3)() at /usr/lib64/python3.6/asyncio/tasks.py:622]>
2018-12-14T10:45:39 Task was destroyed but it is pending!

I also tried with something like

  async def get_things(self, delay_sec: float):
        loop = asyncio.get_event_loop()
        print("working")  # download
        loop.call_later(delay_sec, functools.partial(self.get_things,
                                                     delay_sec))

but then it runs only once and complains the coroutine was never awaited.

Any advice on either how to use the infinite loop and not have problems on quit or doing something else like call_later (even if this is less performant because of the creation of task for each iteration)

Thanks a lot

Zaar Hai

unread,
Dec 14, 2018, 5:07:45 AM12/14/18
to autob...@googlegroups.com
Your first snippet looks fine to me. on_join callback can run as long as required (by awaiting other stuff). 

Others may have better ideas, but I would try to create a small but complete reproduction that you can post here (and may be find a bug along the way :).

Cheers,
Zaar

--
You received this message because you are subscribed to the Google Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send an email to autobahnws+...@googlegroups.com.
To post to this group, send email to autob...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/autobahnws/3c042cb5-5b02-4b6d-9976-8e7290656983%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages