1

I have a project that includes me using some SQL, I am wondering how can I call a function in the DB file?

class testing_db(commands.Cog):
    def __init__(self, bot):
        self.bot=bot
    async def create_db_pool():
        conn = await asyncpg.create_pool(database="db", user="user", host="nunyabusiness", password=DB_PW)



    async def get_account_id(other_id):
       mhm = "Do whatever I do here (I know how to do it but not call it from another file)"






    loop.run_until_complete(create_db_pool())

def setup(bot):
    bot.add_cog(testing_db(bot))

1 Answers1

0

I'm not sure if this is the best/right way, but this is the first result on google so I wanted to share my workaround.

Add a function called run_main or whatever you want to call it. The function just needs to return your async run command:

def run_main():

    return asyncio.run(main())

You can then call the run_main() function as normal from outside this file. Of course, substitute main() as necessary.

In your case, I would assume it's along the lines of adding a method like this:

def run_get_account_id(other_id):

    return get_account_id(other_id)

Then import that method like normal. Note that this is probably a blocking call. I'm feeding a list to my main() so all the async stuff is happening inside main(). I would assume if you call run_get_account_id() individually, you will not get async behavior.

Patrick Conwell
  • 630
  • 1
  • 5
  • 10