Home > Software design >  Pass async function (changes channel name) into thread
Pass async function (changes channel name) into thread

Time:04-20

I'm trying to pass an async function into a @tasks.loop thread since my commands don't work until the task is finished, if threading isn't used. However the task I'm trying to do every 5 minutes is to change the channel name, and it doesn't work. I get a Timeout context manager should be used error.


async def change_name()
  channel = client.get_channel(chid)
  await chid.edit(name='yes')

@tasks.loop(minutes=5)
async def loop():
  _thread = threading.Thread(target=asyncio.run,args=())
  _thread.start()

loop.start()

CodePudding user response:

You can try python-worker for it (link)

from worker import async_worker

@async_worker
async def change_name()
  channel = client.get_channel(chid)
  await chid.edit(name='yes')

@tasks.loop(minutes=5)
async def loop():
    await change_name()

loop.start()

your change_name will be run as thread automatically

  • Related