When does a finished async task release its RAM? #14286
Replies: 3 comments
-
|
Give this a test: import asyncio
import time
async def task(x):
print(f"Task {x} running")
data = bytes(100000)
await asyncio.sleep(1)
print(f"Task {x} done")
return x
async def main():
t = None
while True:
await asyncio.sleep_ms(500)
if t is None:
t = asyncio.create_task(task(time.time()))
print("Created task")
if t.done():
result = await t
print(f"Task {result} joined")
t = None
gc.collect()
print("Free memory", gc.mem_free())
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
-
|
A Your code should be safe. |
Beta Was this translation helpful? Give feedback.
-
|
An alternative way of coding this, perhaps more in tune with the Zen of realtime programming, is to use a queue to link the producer and consumer. Something like: from primitives import RingbufQueue # Microcontroller-optimised version of asyncio.Queue
q = RingbufQueue(10)
tsk = None # Global reference for cancellation
async def producer():
global tsk
while True:
tsk = asyncio.create_task(my_task)
res = await tsk
if res is not None: # Task was not cancelled
await q.put(res)
async def consumer():
asyncio.create_task(producer())
async for result in q:
# process resultAssumes that |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an async task that I need to run repeatedly, but only one instance of it will ever be running at one time. I want to make sure that when the task is finished (either because it finished by itself, or was cancelled), it deallocates any RAM it was using, so that I don't end up running out of RAM.
My code is roughly:
Does awaiting the task object after it's done/cancelled release any RAM the task was using?
Beta Was this translation helpful? Give feedback.
All reactions