Reputation: 33
I am looking to assign a custom name/identifier to a gevent greenlet. Gevent already assigns a unique name:
def name(self):
"""
The greenlet name. By default, a unique name is constructed using
the :attr:`minimal_ident`. You can assign a string to this
value to change it. It is shown in the `repr` of this object.
.. versionadded:: 1.3a2
"""
However, I am not sure how to change this value to a user inputted name. Is it possible to do so?
I tried to do something like this and ended up with an attribute error:
def begin(self):
self.thread = gevent.spawn(self.print_message)
self.thread.minimal_ident = "t1"
print(self.thread.name)
AttributeError: attribute 'minimal_ident' of
'gevent._greenlet.Greenlet' objects is not writable
Upvotes: 3
Views: 646
Reputation: 16624
gevent.Greenlet.name
is not a common property
, but a gevent.util.readproperty
obj, which is a special non-data descriptor works like @property
, and for a non-data descriptor:
... In contrast, non-data descriptors can be overridden by instances.
you could override it simply:
>>> gr = gevent.spawn(gevent.sleep, (1,))
>>> gr.name = "a_cool_name"
>>> print(gr)
<Greenlet "a_cool_name" at 0x1082a47b8: sleep((1,))>
read more on the gevent source and the descriptor doc.
Upvotes: 3
Reputation: 2981
Instantiate the class first and then overwrite the method as such;
my_obj = Class() #Instantiate the class
my_obj.name = 'this is the string'
This will replace the constructed name with your own string.
Upvotes: 1