0

I am trying to make my discord bot respond to more than one person at a time. One of my functions interacts with the spacy module and process big chunks of text. If the function is called once, and once again it ultimately ends up freezing up my bot because it is trying to process the first request.

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')

My question: How can I run a similar function with multiple request at the same time or what am I doing wrong here?

exe
  • 354
  • 3
  • 23
  • 1
    You need to look for a library that supports asynchronous programming. Naively said you can recognize them by the use of `async` and `await` keywords. – Tin Nguyen Apr 06 '20 at 06:59
  • 1
    If no such library exists then you can try this: https://stackoverflow.com/questions/53587063/using-subprocess-to-avoid-long-running-task-from-disconnecting-discord-py-bot/53597795#53597795 – Benjin Apr 06 '20 at 07:29

1 Answers1

1

I fixed this by creating another async function and it would be called when the main event was triggered.

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)

Now you can have multiple request parallel to a function.

exe
  • 354
  • 3
  • 23