7

I'm trying to create a sort of timer callback to a bound async method using asyncio's event loop. The problem now is that the bound async method should not hold a strong reference to the instance, otherwise the latter will never be deleted. The timer callback should only live as long as the parent instance. I've found a solution, but I don't think it's pretty:

import asyncio
import functools
import weakref

class ClassWithTimer:
    def __init__(self):
        asyncio.ensure_future(
            functools.partial(
                ClassWithTimer.update, weakref.ref(self)
            )()
        )

    def __del__(self):
        print("deleted ClassWithTimer!")

    async def update(self):
        while True:
            await asyncio.sleep(1)
            if self() is None: break
            print("IN update of object " + repr(self()))

async def run():
    foo = ClassWithTimer()
    await asyncio.sleep(5)
    del foo

loop = asyncio.get_event_loop()
loop.run_until_complete(run())

Is there a better, more pythonic way to do this? The timer callback really needs to be async. Without asyncio, weakref.WeakMethod would probably be the way to go. But asyncio.ensure_future requires a coroutine object, so it won't work in this case.

pumphaus
  • 91
  • 1
  • 6
  • Any reason not to stop the timer explicitly by cancelling the future returned by `ensure_future`? – Vincent Oct 19 '15 at 16:04
  • That would be possible if `__del__` would be called at all. But when I do `self.timer = asyncio.ensure_future(self.update())`, then the event loop holds a strong ref to the instance, preventing a call to `__del__` in the first place. – pumphaus Oct 19 '15 at 16:10

4 Answers4

2

Sorry, I'm not sure if I understood your question correctly. Is this solution you're looking for?

import asyncio


class ClassWithTimer:
    async def __aenter__(self):
        self.task = asyncio.ensure_future(self.update())

    async def __aexit__(self, *args):
        try:
            self.task.cancel()  # Just cancel updating when we don't need it.
            await self.task
        except asyncio.CancelledError:  # Ignore CancelledError rised by cancelled task.
            pass
        del self  # I think you don't need this in real life: instance should be normally deleted by GC.

    def __del__(self):
        print("deleted ClassWithTimer!")

    async def update(self):
        while True:
            await asyncio.sleep(1)
            print("IN update of object " + repr(self))


async def run():
    async with ClassWithTimer():  # Use context manager to handle when we need updating.
        await asyncio.sleep(5)


loop = asyncio.get_event_loop()
loop.run_until_complete(run())

Output:

IN update of object <__main__.ClassWithTimer object at 0x000000468D0FB208>
IN update of object <__main__.ClassWithTimer object at 0x000000468D0FB208>
IN update of object <__main__.ClassWithTimer object at 0x000000468D0FB208>
IN update of object <__main__.ClassWithTimer object at 0x000000468D0FB208>
deleted ClassWithTimer!
[Finished in 5.2s]

One more way without context manager:

import asyncio


class ClassWithTimer:
    def __init__(self):
        self.task = asyncio.ensure_future(self.update())

    async def release(self):
        try:
            self.task.cancel()
            await self.task
        except asyncio.CancelledError:
            pass
        del self

    def __del__(self):
        print("deleted ClassWithTimer!")

    async def update(self):
        while True:
            await asyncio.sleep(1)
            print("IN update of object " + repr(self))


async def run():
    foo = ClassWithTimer()
    await asyncio.sleep(5)
    await foo.release()


loop = asyncio.get_event_loop()
loop.run_until_complete(run())
Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
  • It would do the job, but I would like to avoid using context management just for a timer. – pumphaus Oct 19 '15 at 18:49
  • @pumphaus I updated answer and added code, that doesn't use context manager. But you should call "release" function in that case. If you want to delete object, you *should* somehow stop running task. I believe to use context manager - the best way to do it. – Mikhail Gerasimov Oct 19 '15 at 19:21
2

I came across almost the same problem few months ago, and I wrote a decorator to get around it:

def weakmethod(f):
    @property
    def get(self):
        return f.__get__(weakref.proxy(self))
#       self = weakref.proxy(self)
#       if hasattr(f, '__get__'):
#            raise RuntimeWarning(
#                'weakref may not work unless you implement '
#                'the property protocol carefully by youself!'
#            )
#           return f.__get__(self)
#       if asyncio.iscoroutinefunction(f):
#           #Make the returned method a coroutine function, optional
#           async def g(*arg, **kwarg):
#               return await f(self, *arg, **kwarg)
#       else:
#           def g(*arg, **kwarg):
#               return f(self, *arg, **kwarg)
#       return g
#       #Still some situations not taken into account?
    return get

Your code could then be rewritten in much a natural way:

class ClassWithTimer:
    def __init__(self):
        asyncio.ensure_future(self.update())

    def __del__(self):
        print("deleted ClassWithTimer!")

    @weakmethod
    async def update(self):
        while True:
            await asyncio.sleep(1)
            print("IN update of object ", self)

Important:

  • weakref.proxy does not prevent you from acquiring a strong reference. In addition, it is not guaranteed to behave exactly the same as the original object.

  • My implementation has not covered all possibilities.

Huazuo Gao
  • 1,603
  • 14
  • 20
  • This is pretty much what I need (even though I'd implement it without the decorator). However, when the instance is deleted and the proxy object is invalidated, it raises a ReferenceException upon next use. I guess I can write a wrapper method that checks for validity by trying to access proxy.__weakref__ and catching the Exception in case it's invalidated. But the proper way would probably be via the callback mechanism of weakref.proxy. – pumphaus Oct 19 '15 at 18:58
1

Based on the answers from Huazuo Gao and germn I implemented ensure_weakly_binding_future which is basically the same as ensure_future but does not keep a strong reference to the instance of the bound method. It does not modify the overall binding (like the decorator-based solution does) and properly cancels the future when the parent instance is deleted:

import asyncio
import weakref

def ensure_weakly_binding_future(method):
    class Canceller:
        def __call__(self, proxy):
            self.future.cancel()

    canceller = Canceller()
    proxy_object = weakref.proxy(method.__self__, canceller)
    weakly_bound_method = method.__func__.__get__(proxy_object)
    future = asyncio.ensure_future(weakly_bound_method())
    canceller.future = future

class ClassWithTimer:
    def __init__(self):
        ensure_weakly_binding_future(self.update)

    def __del__(self):
        print("deleted ClassWithTimer!", flush=True)

    async def update(self):
        while True:
            await asyncio.sleep(1)
            print("IN update of object " + repr(self), flush=True)

async def run():
    foo = ClassWithTimer()
    await asyncio.sleep(5.5)
    del foo
    await asyncio.sleep(2.5)

loop = asyncio.get_event_loop()
loop.run_until_complete(run())
pumphaus
  • 91
  • 1
  • 6
0
  1. Python 3.5 handles cycle references very well (see PEP 442 https://www.python.org/dev/peps/pep-0442/)

  2. I'll push timeout async context manager into asyncio soon, see the draft:

`

import asyncio
import warnings


class Timeout:
    def __init__(self, timeout, *, raise_error=False, loop=None):
        self._timeout = timeout
        if loop is None:
            loop = asyncio.get_event_loop()
        self._loop = loop
        self._raise_error = raise_error
        self._task = None
        self._cancelled = False
        self._cancel_handler = None

    async def __aenter__(self):
        self._task = asyncio.Task.current_task(loop=loop)
        self._cancel_handler = self._loop.call_later(
            self._cancel, self._timeout)

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if self._cancelled:
            if self._raise_error:
                raise asyncio.TimeoutError
            else:
                # suppress
                return True
        else:
            self._cancel_handler.cancel()
        # raise all other errors

    def __del__(self):
        if self._task:
            # just for preventing improper usage
            warnings.warn("Use async with")

    def _cancel(self):
        self._cancelled = self._task.cancel()


async def long_running_task():
    while True:
        asyncio.sleep(5)


async def run():
    async with Timeout(1):
        await long_running_task()


loop = asyncio.get_event_loop()
loop.run_until_complete(run())
Andrew Svetlov
  • 16,730
  • 8
  • 66
  • 69