Answer the question
In order to leave comments, you need to log in
How to modify the method so that it runs in the background?
Please tell me how to change the start method. So that it runs in the background, and when asyncio.sleep is called, not all the code branch that called it falls asleep, and the start method returns control back so that create_timer and func continue execution. And when asyncio.sleep finished, the start function continued and ran in the background.
The problem is that the start method blocks the entire branch where it was called, instead of executing in the background.
This is an example incomplete piece of code for clarity:
...
class ClassName():
async def func(self):
...
await timer_manager.create_timer(args...)
...
class TimerManager():
_timers = {}
time = 0
def __init__(self, time):
self.time = time
async def create_timer(self, args...):
self._timers[args...] = Timer(self.time, args...)
await self._timers[args...].start()
async def delete_timer(self, args...):
try:
...
self._timers.pop(args...)
except:
pass
class Timer():
time = 0
...
def __init__(self, time, args...):
self.time = time
...
async def start(self):
await asyncio.sleep(self.time)
...
...
timer_manager = TimerManager(args...)
...
Answer the question
In order to leave comments, you need to log in
You call start as a simple function await self._timers[args...].start()
, naturally it will block the stack and wait for the end, read about create_task in asincio and coroutines in general, and use it when creating tasks to start the timer, and not just calling start
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question