0

I'm currently trying to create a script which handles multiple Blender command prompt renders in a queue, while at the same running a discord bot in the background which responds with the elapsed time ETA and other stuff when using a !progress command.

However, when I tried running them one after another, it seemed that the program doesn't do anything more after the bot is started. When I looked online it said that it is because it's an event loop and before the bot function gets stopped the code does not proceed.

Here is my code if anyone asks later:

Discord bot:

import asyncio
import discord
from discord.ext import commands

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')

@bot.command()
async def progress(ctx):
    progress_message = 'SAMPLE PROGRESS MESSAGE'
    await ctx.send(progress_message)

async def run_bot():
    await bot.start('BOT_TOKEN_HERE')

def main():
    bot.run('YOUR_DISCORD_BOT_TOKEN')

if __name__ == '__main__':
    main()

The portion which handles the rendering is a giant while loop which waits until one file is finished, moves on to the next one, and so on until at the end of rendering a file the number of the file is equal to the total number of renders (I can share the code if someone wants to)

I tried using 'asyncio.gather()', but this is my first time using asyncio so it either gave me an error "... attached to a different loop" or the programmes ran sequentially, which as I described already does not work.

I don't know where to go from here, that's why I'm asking this question.

Any help will be appreciated, even if someone links a (suitable) tutorial.

  • Use multi-threading. – Barmar May 19 '23 at 21:10
  • @Barmar Can you expound? – EverythingerTruten May 19 '23 at 21:12
  • Sorry, not really. It's just general understanding that you use multi-threading to do two things at once. – Barmar May 19 '23 at 21:13
  • https://superfastpython.com/python-asyncio/ This is a great resource if you specifically want to use async to achieve what you want. What I'm guessing is happening is that one of the async functions you are calling is always blocking (actively doing something), so the other async task never gets a chance to start. That's where the await calls in your async functions come in, they pause the current task because you're waiting on something (usually I/O), and the other tasks can do work in the meantime. – Jared Dobry May 19 '23 at 22:27

1 Answers1

0

You can use a thread. It basically execute multiple things at the same time. The main program will execute in a main thread. You would have to use the threading module. Here is an example of threading:

def hello():
    print("hi")
thread1 = threading.Thread(target=hello, args=())
thread1.start()

It would keep printing "hi" during your main program runs. You can use this to do other functions as your main program runs. Just replace target with the target function and add args if needed.

Darkshadogt
  • 49
  • 1
  • 5