Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

主动发言的性能问题 #21

Open
yaowan233 opened this issue Jan 25, 2023 · 8 comments
Open

主动发言的性能问题 #21

yaowan233 opened this issue Jan 25, 2023 · 8 comments
Labels
enhancement New feature or request

Comments

@yaowan233
Copy link

现在每三分钟都要从数据库获取一天群内所有消息(
这部分能不能做成缓存,搞个缓存记录一下最近的消息,不然太吃cpu了

for group_id in groups:
if messages := await ChatMessage.filter(group_id=group_id, time__gte=today_time):
total_messages[group_id] = messages

@CMHopeSunshine
Copy link
Owner

还好吧,几万条数据也都不到0.5秒就获取完,如果要搞缓存,那就是内存换cpu了,我觉得暂时不必

@yaowan233
Copy link
Author

但我这服务器如果开着主动发言的话cpu一直爆满(

@CMHopeSunshine
Copy link
Owner

是内存爆还是cpu爆啊,我的服务器2核4g,cpu占用长期不到15%,我觉得不是这个的问题,内存占用大倒是真的,因为用到了jieba库,本插件会有200m左右的内存占用

@yaowan233
Copy link
Author

没 就是cpu 我服务器是单核2.5g的

@yaowan233
Copy link
Author

我只要把定时任务关了就没问题了 肯定不是jieba库的问题

@CMHopeSunshine
Copy link
Owner

这样的话建议试试用原版牛牛吧,它是把消息存内存里定期做持久化的,我在复刻它的时候就是想去掉缓存以优化内存占用

@CMHopeSunshine CMHopeSunshine added the wontfix This will not be worked on label Jan 25, 2023
@yaowan233
Copy link
Author

yaowan233 commented Jan 25, 2023

好吧 找到问题了
问题是有一万多个context,但是在这个群里找不到一个answer,所以一直在进行数据库操作(

for context in contexts:
if (not speak_list or random.random() < config.speak_continuously_probability) and len(
speak_list) < config.speak_continuously_max_len:
if answers := await ChatAnswer.filter(context=context,
group_id=group_id,
count__gte=config.answer_threshold):
answer = random.choices(answers,
weights=[
answer.count + 1 if answer.time >= today_time else answer.count
for answer in answers])[0]
message = random.choice(answer.messages)
if len(message) < 2:
continue
if message.startswith('&#91;') and message.endswith('&#93;'):
continue
if any(word in message for word in ban_words):
continue
speak_list.append(message)
follow_answer = answer
while random.random() < config.speak_continuously_probability and len(
speak_list) < config.speak_continuously_max_len:
if (follow_context := await ChatContext.filter(
keywords=follow_answer.keywords).first()) and (
follow_answers := await ChatAnswer.filter(
group_id=group_id,
context=follow_context,
count__gte=config.answer_threshold)):
follow_answer = random.choices(follow_answers,
weights=[
a.count + 1 if a.time >= today_time else a.count
for a in follow_answers])[0]
message = random.choice(follow_answer.messages)
if len(message) < 2:
continue
if message.startswith('&#91;') and message.endswith('&#93;'):
continue
if all(word not in message for word in ban_words):
speak_list.append(message)
else:
break
else:
break

@yaowan233
Copy link
Author

yaowan233 commented Jan 25, 2023

就是在其他群里面学到了很多,但就是有个群里没有学到什么,这时候就一直在这个for里面循环了
建议可以直接查询answer表,这样可以省下很多时间

@CMHopeSunshine CMHopeSunshine added enhancement New feature or request and removed wontfix This will not be worked on labels Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants