Skip to content

Commit

Permalink
Add decorator: queued_up
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyaowong committed Oct 28, 2020
1 parent 31bcb0f commit 9de91c0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion botoy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pylint: disable=C0415,C0413
__version__ = '0.0.12'
__version__ = '0.0.13'


def check_version():
Expand Down
1 change: 1 addition & 0 deletions botoy/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions botoy/decorators/_queued_up.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion docs/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- bot 端的内置队列貌似还不太稳定,必要时请自行设置延时,但又由于接收函数之间和每次接收到新消息后,运行都是同时进行的,
由此导致的频率和数据安全问题,请自行解决。旧版库[python-iotbot](https://github.com/xiyaowong/python--iotbot)里的 Action 提供了队列发送功能

版本 `0.0.13`提供了`queued_up`装饰器让函数实现简单的排队执行功能

- botoy.collection
模块封装了已知的消息类型(MsgTypes)和事件类型(EventNames)以及部分表情代码(Emoticons)

Expand All @@ -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),用法差不多,只是方法名或参数名有一点不同,迁移很方便
4 changes: 4 additions & 0 deletions docs_src/decorators_outline.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ def from_phone(func=None):
def from_admin(func=None):
"""来自群管理员(列表包括群主)的消息 GroupMsg
管理员列表会进行``缓存``,调用520次后再次刷新, 所以可以放心使用"""


def queued_up(func=None):
"""队列执行函数, 所有被包装的函数共用同一个队列, 该装饰器适用于所有函数"""

0 comments on commit 9de91c0

Please sign in to comment.