Discussion:
[gevent] Subclassing Greenlet
i***@zaleos.net
2018-10-10 10:54:16 UTC
Permalink
I am trying to subclass Greenlet to provide a uniform interface for the
tests executed inside each of the greenlets. I checked gevent docs
<http://www.gevent.org/api/gevent.greenlet.html#subclassing-greenlet> and gevent
source code
<https://github.com/gevent/gevent/blob/master/src/gevent/greenlet.py#L216> and
attempted the following solution in order to measure overall execution time
of the greenlet:

from gevent import Greenlet
class MyGreenlet(Greenlet):
""" Tweak to properly store the args when this greenlet was created.
"""
def __init__(self, *args, **kwargs):
super(MyGreenlet, self).__init__(*args, **kwargs)
self._myargs = args
self._mykwargs = kwargs
self.duration = 0
self.func = None

# Custom _run() method to capture start/stop time
def _run(self, *args, **kwargs):
print('Run start')
start = time.time()
self.func(*args, **kwargs)
end = time.time()
self.duration = end - start
print('Run end')

def __setattr__(self, name, value):
if name == '_run':
self.func = value


When I attempt to execute above code, it fails with

AttributeError: 'MyGreenlet' object has no attribute 'func'

inside overriden _run() method when calling self.func(*args, **kwargs)

If I debug above code, I expect to see custom properties being added to the
object instance as the constructor executes, but that doesn't happen.

Feel like I'm missing something basic here. Any ideas?
--
You received this message because you are subscribed to the Google Groups "gevent: coroutine-based Python network library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gevent+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason Madden
2018-10-10 11:15:16 UTC
Permalink
Post by i***@zaleos.net
from gevent import Greenlet
""" Tweak to properly store the args when this greenlet was created.
"""
super(MyGreenlet, self).__init__(*args, **kwargs)
self._myargs = args
self._mykwargs = kwargs
self.duration = 0
self.func = None
# Custom _run() method to capture start/stop time
print('Run start')
start = time.time()
self.func(*args, **kwargs)
end = time.time()
self.duration = end - start
print('Run end')
self.func = value
When I attempt to execute above code, it fails with
AttributeError: 'MyGreenlet' object has no attribute 'func'
inside overriden _run() method when calling self.func(*args, **kwargs)
If I debug above code, I expect to see custom properties being added to the object instance as the constructor executes, but that doesn't happen.
Feel like I'm missing something basic here. Any ideas?
You seem to be neglecting to call `super().__setattr__(name, value)` in your `__setattr__` implementation (in a missing `else:` block). If I do that, I see the expected items in the `__dict__`.

Beware that in `__init__`, calling `super(MyGreenlet, self).__init__(...)` will set `_run`, which invokes your `__setattr__` to set `func`, but then your `__init__` replaces that value with `None`.
--
You received this message because you are subscribed to the Google Groups "gevent: coroutine-based Python network library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gevent+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...