我希望为 gevent greenlet 分配自定义名称/标识符。 Gevent 已经分配了一个唯一的名称:
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
"""
但是,我不确定如何将此值更改为用户输入的名称。有可能这样做吗?
我试图做这样的事情并以属性错误结束:
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
gevent.Greenlet.name
不是一个普通的property
,而是一个gevent.util.readproperty
obj,即a special non-data descriptor像 @property
一样工作,对于非数据描述符:
... In contrast, non-data descriptors can be overridden by instances.
你可以简单地覆盖它:
>>> gr = gevent.spawn(gevent.sleep, (1,))
>>> gr.name = "a_cool_name"
>>> print(gr)
<Greenlet "a_cool_name" at 0x1082a47b8: sleep((1,))>
阅读更多关于 gevent 源代码和 the descriptor doc 的信息.
我是一名优秀的程序员,十分优秀!