Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a callback to to call _reaper instead of scheduling it #45

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions dbussy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io
import atexit
import asyncio
import functools
from xml.etree import \
ElementTree as XMLElementTree
from xml.sax.saxutils import \
Expand Down Expand Up @@ -1454,32 +1455,20 @@ class TaskKeeper :
def _init(self) :
# avoid __init__ so I don't get passed spurious args
self.loop = None
self._cur_tasks = []
self._cur_tasks = set()
#end _init

def create_task(self, coro) :
assert self.loop != None, "no event loop to attach coroutine to"
task = self.loop.create_task(coro)
if len(self._cur_tasks) == 0 :
self.loop.call_soon(self._reaper, weak_ref(self))
#end if
self._cur_tasks.append(task)
task.add_done_callback(functools.partial(self._reaper, weak_ref(self)))
self._cur_tasks.add(task)
#end create_task

@staticmethod
def _reaper(self) :
def _reaper(self, task) :
self = self() # avoid reference circularity
old_tasks = self._cur_tasks[:]
new_tasks = self._cur_tasks
new_tasks[:] = []
for task in old_tasks :
if not task.done() :
new_tasks.append(task)
#end if
#end for
if len(new_tasks) != 0 :
self.loop.call_soon(self._reaper, weak_ref(self))
#end if
self._cur_tasks.remove(task)
#end _reaper

#end TaskKeeper
Expand Down