【问题标题】:Bot can't handle multiple request parallel to function (discord.py)Bot 无法处理与功能并行的多个请求 (discord.py)
【发布时间】:2020-07-18 00:57:00
【问题描述】:

我正试图让我的不和谐机器人一次响应多个人。我的一个功能与 spacy 模块交互并处理大块文本。如果该函数被调用一次,它最终会冻结我的机器人,因为它正在尝试处理第一个请求。

nlp = spacy.load("en_core_web_sm")

@bot.event
async def on_message(message):
   if message.content.startswith('!research'):

       doc = 'long paragraphs...'

      #Searches through doc for sentences relating to a word
       nlp(doc) #Takes time to process here
       results = textacy.extract.semistructured_statements(document, 'a word')

我的问题:如何同时运行具有多个请求的类似功能,或者我在这里做错了什么?

【问题讨论】:

  • 你需要寻找支持异步编程的库。天真地说你可以通过使用asyncawait关键字来识别它们。
  • 如果不存在这样的库,那么你可以试试这个:stackoverflow.com/questions/53587063/…

标签: python discord discord.py spacy


【解决方案1】:

我通过创建另一个异步函数来解决这个问题,它会在主事件被触发时被调用。

nlp = spacy.load("en_core_web_sm")

#Use async here!
async def researchCommand(doc, message): #Takes in doc and message so we can respond to author
    #Does processing here
    nlp(doc)


@bot.event
async def on_message(message): #Main function

    big_book = 'long paragraphs...'

    if message.content.startswith('!research'):
        await researchCommand(big_book, message)

现在您可以将多个请求与一个函数并行。

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 2021-10-04
    • 1970-01-01
    • 2018-01-17
    • 2023-01-30
    • 2021-12-17
    • 2020-09-06
    • 1970-01-01
    • 2013-06-12
    相关资源
    最近更新 更多