diff --git a/botoy/__version__.py b/botoy/__version__.py index 0a6296de..265d1da9 100644 --- a/botoy/__version__.py +++ b/botoy/__version__.py @@ -1,5 +1,5 @@ # pylint: disable=C0415,C0413 -__version__ = '0.0.12' +__version__ = '0.0.13' def check_version(): diff --git a/botoy/decorators/__init__.py b/botoy/decorators/__init__.py index fd133e29..9da1b513 100644 --- a/botoy/decorators/__init__.py +++ b/botoy/decorators/__init__.py @@ -10,6 +10,7 @@ from ._ignore_these_groups import ignore_these_groups from ._ignore_these_users import ignore_these_users from ._in_content import in_content +from ._queued_up import queued_up from ._startswith import startswith from ._these_msgtypes import these_msgtypes from ._with_pattern import with_pattern diff --git a/botoy/decorators/_queued_up.py b/botoy/decorators/_queued_up.py new file mode 100644 index 00000000..24fdabb4 --- /dev/null +++ b/botoy/decorators/_queued_up.py @@ -0,0 +1,24 @@ +import time +from threading import Lock + +_lock = Lock() + + +def queued_up(func=None): + """队列执行函数, 所有被包装的函数共用同一个队列, 该装饰器适用于所有函数""" + if func is None: + return queued_up + + def inner(ctx): + try: + _lock.acquire() + ret = func(ctx) + # 为了易用性,这里的延时大小不开放出来 + # 一般情况下只要不是`同时`发起的请求都是能够成功的 + # 这里设置少量的延时进一步提高成功率 + time.sleep(0.5) + return ret + finally: + _lock.release() + + return inner diff --git a/docs/other.md b/docs/other.md index 5c65a4af..6137b3df 100644 --- a/docs/other.md +++ b/docs/other.md @@ -5,6 +5,8 @@ - bot 端的内置队列貌似还不太稳定,必要时请自行设置延时,但又由于接收函数之间和每次接收到新消息后,运行都是同时进行的, 由此导致的频率和数据安全问题,请自行解决。旧版库[python-iotbot](https://github.com/xiyaowong/python--iotbot)里的 Action 提供了队列发送功能 + 版本 `0.0.13`提供了`queued_up`装饰器让函数实现简单的排队执行功能 + - botoy.collection 模块封装了已知的消息类型(MsgTypes)和事件类型(EventNames)以及部分表情代码(Emoticons) @@ -13,4 +15,4 @@ - 私聊消息比较难处理,如果好友消息 ctx 的 TempUin 不为 None,说明是私聊消息, 这个字段是私聊入口群号, 可以用 `ensure_tempMsg`接收函数装饰器确保是私聊消息 -- [示例](https://github.com/xiyaowong/python--iotbot/tree/master/sample),用法差不多,只是方法名或参数名有一点不同,迁移很方便 +- 点击查看[示例](https://github.com/xiyaowong/python--iotbot/tree/master/sample),用法差不多,只是方法名或参数名有一点不同,迁移很方便 diff --git a/docs_src/decorators_outline.py b/docs_src/decorators_outline.py index 60ac362c..325463b4 100644 --- a/docs_src/decorators_outline.py +++ b/docs_src/decorators_outline.py @@ -70,3 +70,7 @@ def from_phone(func=None): def from_admin(func=None): """来自群管理员(列表包括群主)的消息 GroupMsg 管理员列表会进行``缓存``,调用520次后再次刷新, 所以可以放心使用""" + + +def queued_up(func=None): + """队列执行函数, 所有被包装的函数共用同一个队列, 该装饰器适用于所有函数"""