blob: 73b7db0d336ac783219f0aece3fdaa810b33a10c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# Copyright 2012-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
import os
import sys
from .EventLoop import EventLoop
from portage.util._eventloop.asyncio_event_loop import AsyncioEventLoop
_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 = AsyncioEventLoop
# If the default constructor doesn't support multiprocessing,
# then multiprocessing constructor is used in subprocesses.
if not constructor.supports_multiprocessing and pid != _MAIN_PID:
constructor = EventLoop
# Use the _asyncio_wrapper attribute, so that unit tests can compare
# the reference to one retured from _wrap_loop(), since they should
# not close the loop if it refers to a global event loop.
instance = constructor()._asyncio_wrapper
_instances[pid] = instance
return instance
|