From 56337b00ea46d96037037816d7b5c7904825b8a9 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Thu, 9 Feb 2012 12:04:42 -0800 Subject: Add global_event_loop() and GlibEventLoop. This causes all PollScheduler instances within a given process to share a singleton EventLoop instance, and also makes it possible to swap in glib's main loop for all portage event loops in the main process. --- pym/portage/util/_eventloop/global_event_loop.py | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 pym/portage/util/_eventloop/global_event_loop.py (limited to 'pym/portage/util/_eventloop/global_event_loop.py') diff --git a/pym/portage/util/_eventloop/global_event_loop.py b/pym/portage/util/_eventloop/global_event_loop.py new file mode 100644 index 000000000..0f0c53f2b --- /dev/null +++ b/pym/portage/util/_eventloop/global_event_loop.py @@ -0,0 +1,36 @@ +# Copyright 2012 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +import os + +from portage.util._eventloop.EventLoop import EventLoop + +_default_constructor = EventLoop +#from portage.util._eventloop.GlibEventLoop \ +# import GlibEventLoop as _default_constructor + +# If _default_constructor doesn't support multiprocessing, +# then _multiprocessing_constructor is used in subprocesses. +_multiprocessing_constructor = EventLoop + +_MAIN_PID = os.getpid() +_instances = {} + +def global_event_loop(): + """ + Get a global EventLoop (or compatible object) instance which + belongs exclusively to the current process. + """ + + pid = os.getpid() + instance = _instances.get(pid) + if instance is not None: + return instance + + constructor = _default_constructor + if not constructor.supports_multiprocessing and pid != _MAIN_PID: + constructor = _multiprocessing_constructor + + instance = constructor() + _instances[pid] = instance + return instance -- cgit v1.2.3-1-g7c22