From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1Rux3K-0005dp-Dn for garchives@archives.gentoo.org; Wed, 08 Feb 2012 02:14:34 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id BC1B6E081E; Wed, 8 Feb 2012 02:14:26 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 7E97FE081E for ; Wed, 8 Feb 2012 02:14:26 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id E55251B4035 for ; Wed, 8 Feb 2012 02:14:25 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 303C880043 for ; Wed, 8 Feb 2012 02:14:25 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <32e3bffa19329a848633a7fcefbf4fe3761dbfef.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: pym/_emerge/ X-VCS-Repository: proj/portage X-VCS-Files: pym/_emerge/PollScheduler.py X-VCS-Directories: pym/_emerge/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: 32e3bffa19329a848633a7fcefbf4fe3761dbfef Date: Wed, 8 Feb 2012 02:14:25 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: 621a8f30-069b-4279-b90a-42f48e6de9d0 X-Archives-Hash: ed8748aae0d61834158705dcdaff716c commit: 32e3bffa19329a848633a7fcefbf4fe3761dbfef Author: Zac Medico gentoo org> AuthorDate: Wed Feb 8 01:59:12 2012 +0000 Commit: Zac Medico gentoo org> CommitDate: Wed Feb 8 02:12:08 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/portage.git;a= =3Dcommit;h=3D32e3bffa PollScheduler: implement idle_add --- pym/_emerge/PollScheduler.py | 46 ++++++++++++++++++++++++++++++++++++= +++++- 1 files changed, 45 insertions(+), 1 deletions(-) diff --git a/pym/_emerge/PollScheduler.py b/pym/_emerge/PollScheduler.py index 7cf8189..ab18f0d 100644 --- a/pym/_emerge/PollScheduler.py +++ b/pym/_emerge/PollScheduler.py @@ -24,9 +24,13 @@ from _emerge.PollSelectAdapter import PollSelectAdapte= r class PollScheduler(object): =20 class _sched_iface_class(SlotObject): - __slots__ =3D ("io_add_watch", "output", "register", "schedule", + __slots__ =3D ("idle_add", "io_add_watch", + "output", "register", "schedule", "source_remove", "timeout_add", "unregister") =20 + class _idle_callback_class(SlotObject): + __slots__ =3D ("args", "callback", "source_id") + class _io_handler_class(SlotObject): __slots__ =3D ("args", "callback", "fd", "source_id") =20 @@ -45,6 +49,7 @@ class PollScheduler(object): self._poll_event_handler_ids =3D {} # Increment id for each new handler. self._event_handler_id =3D 0 + self._idle_callbacks =3D {} self._timeout_handlers =3D {} self._timeout_interval =3D None self._poll_obj =3D create_poll_instance() @@ -52,6 +57,7 @@ class PollScheduler(object): self._scheduling =3D False self._background =3D False self.sched_iface =3D self._sched_iface_class( + idle_add=3Dself._idle_add, io_add_watch=3Dself._register, output=3Dself._task_output, register=3Dself._register, @@ -291,6 +297,35 @@ class PollScheduler(object): =20 return bool(events_handled) =20 + def _idle_add(self, callback, *args): + """ + Like glib.idle_add(), if callback returns False it is + automatically removed from the list of event sources and will + not be called again. + + @type callback: callable + @param callback: a function to call + @rtype: int + @return: an integer ID + """ + self._event_handler_id +=3D 1 + source_id =3D self._event_handler_id + self._idle_callbacks[source_id] =3D self._idle_callback_class( + args=3Dargs, callback=3Dcallback, source_id=3Dsource_id) + return source_id + + def _run_idle_callbacks(self): + if not self._idle_callbacks: + return + # Iterate of our local list, since self._idle_callbacks can be + # modified during the exection of these callbacks. + for x in list(self._idle_callbacks.values()): + if x.source_id not in self._idle_callbacks: + # it got cancelled while executing another callback + continue + if not x.callback(*x.args): + self._unregister(x.source_id) + def _timeout_add(self, interval, function, *args): """ Like glib.timeout_add(), interval argument is the number of @@ -317,6 +352,12 @@ class PollScheduler(object): return source_id =20 def _run_timeouts(self): + + self._run_idle_callbacks() + + if not self._timeout_handlers: + return False + ready_timeouts =3D [] current_time =3D time.time() for x in self._timeout_handlers.values(): @@ -370,6 +411,9 @@ class PollScheduler(object): is found and removed, and False if the reg_id is invalid or has already been removed. """ + idle_callback =3D self._idle_callbacks.pop(reg_id, None) + if idle_callback is not None: + return True timeout_handler =3D self._timeout_handlers.pop(reg_id, None) if timeout_handler is not None: if timeout_handler.interval =3D=3D self._timeout_interval: