asyncio

Łukasz Ligowski orangewarrior at gmail.com
Fri Mar 27 04:33:27 EDT 2015


Hi,

I wrote simple asyncio program (see below) and I'm not sure if I understand
behavior correctly.
I have print_tasks coroutine which prints each task in a loop by using
Task.all_tasks function.
I have also task_launcher coroutine that launches (by loop.create_task())
simple task that waits some and prints "ping".
Question is why finished "ping" tasks keep accumulating in Task.all_tasks?
After a while there is a lot of:
<Task finished coro=<ping() done, defined at test.py:25> result=None>

when printing in Task.all_tasks and number of those increases as new ping
tasks are launched

Is there a way to prune finished tasks (I tried forcing gc) or I do
something wrong?

I ran on python 3.4.3.

Best regards,
L

import asyncio

loop = asyncio.get_event_loop()

@asyncio.coroutine
def print_tasks(loop):
        while True:
                tasks = asyncio.Task.all_tasks(loop)

                for task in tasks:
                        print(task)
                print()

                yield from asyncio.sleep(1)

@asyncio.coroutine
def task_launcher(loop):
        while True:
                yield from asyncio.sleep(1)
                loop.create_task(ping())

@asyncio.coroutine
def ping():
        yield from asyncio.sleep(2)
        print("ping")


loop.create_task(print_tasks(loop))
loop.create_task(task_launcher(loop))

loop.run_forever()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150327/ad55c1c7/attachment.html>


More information about the Python-list mailing list