badProgrammer0123456
badProgrammer0123456

Reputation: 62

How to multiprocess with discord.py?

An example of what I want to do would be something like this

i = 0
@bot.command()
async def IncreaseI(ctx):
    global i
    while True:
        i += 1
        sleep(5)

@bot.command()
async def PrintI(ctx):
    await ctx.send(f"I: {i}")
    

but this doesn't work.

Upvotes: 0

Views: 1170

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

First of all, you're using time.sleep(), that's gonna block your whole code, use await asyncio.sleep() instead. Second of all it's better to create a task, it's a lot easier to manage, here's how:

from discord.ext import tasks

i = 0

@tasks.loop(seconds=5)
async def increaseI():
    global i
    i += 1


increaseI.start() # You can also start in a command

# You can also
increaseI.stop()
increaseI.cancel()
increaseI.restart()
# Read the docs for more methods

Reference

Upvotes: 1

Related Questions