/usr/lib/python2.7/dist-packages/tango/green.py is in python-tango 9.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | # -----------------------------------------------------------------------------
# This file is part of PyTango (http://pytango.rtfd.io)
#
# Copyright 2006-2012 CELLS / ALBA Synchrotron, Bellaterra, Spain
# Copyright 2013-2014 European Synchrotron Radiation Facility, Grenoble, France
#
# Distributed under the terms of the GNU Lesser General Public License,
# either version 3 of the License, or (at your option) any later version.
# See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
__all__ = [
"get_green_mode", "set_green_mode",
"get_executor", "submit", "spawn", "result", "wait",
"get_synch_executor", "synch_submit", "synch_wait",
"get_gevent_executor", "gevent_submit", "gevent_wait",
"get_futures_executor", "futures_submit", "futures_wait",
"get_asyncio_executor", "asyncio_submit", "asyncio_wait",
"get_object_submitter", "get_object_waiter", "get_object_green_mode",
"green", "green_cb", "get_wait_default_value",
"get_event_loop", "get_gevent_event_loop"]
__docformat__ = "restructuredtext"
# Imports
import os
from functools import wraps, partial
# Tango imports
from ._tango import GreenMode
# Gevent imports
from .tango_gevent import get_global_executor as get_gevent_executor
from .tango_gevent import submit as gevent_submit
from .tango_gevent import wait as gevent_wait
from .tango_gevent import get_event_loop as get_gevent_event_loop
# Futures imports
from .tango_futures import get_global_executor as get_futures_executor
from .tango_futures import submit as futures_submit
from .tango_futures import wait as futures_wait
# Asyncio imports
from .tango_asyncio import get_global_executor as get_asyncio_executor
from .tango_asyncio import submit as asyncio_submit
from .tango_asyncio import wait as asyncio_wait
from .tango_asyncio import get_event_loop as get_asyncio_event_loop
# Handle current green mode
__default_green_mode = GreenMode.Synchronous
try:
__current_green_mode = getattr(
GreenMode,
os.environ["PYTANGO_GREEN_MODE"].capitalize())
except Exception:
__current_green_mode = __default_green_mode
def set_green_mode(green_mode=None):
"""Sets the global default PyTango green mode.
Advice: Use only in your final application. Don't use this in a python
library in order not to interfere with the beavior of other libraries
and/or application where your library is being.
:param green_mode: the new global default PyTango green mode
:type green_mode: GreenMode
"""
global __current_green_mode
if __current_green_mode == green_mode:
return
if green_mode == GreenMode.Gevent:
# check if we can change to gevent mode
import tango.gevent
elif green_mode == GreenMode.Futures:
# check if we can change to futures mode
import tango.futures
elif green_mode == GreenMode.Asyncio:
# check if we can change to asyncio mode
import tango.asyncio
__current_green_mode = green_mode
def get_green_mode():
"""Returns the current global default PyTango green mode.
:returns: the current global default PyTango green mode
:rtype: GreenMode
"""
return __current_green_mode
# Synchronous execution
class SynchExecutor(object):
def submit(self, fn, *args, **kwargs):
return fn(*args, **kwargs)
__synch_executor = SynchExecutor()
def get_synch_executor():
return __synch_executor
def synch_submit(fn, *args, **kwargs):
return get_synch_executor().submit(fn, *args, **kwargs)
def synch_wait(res, timeout=None):
return res
# Excutor, submitter and waiter mapping
__executor_map = {
GreenMode.Synchronous: get_synch_executor,
GreenMode.Futures: get_futures_executor,
GreenMode.Gevent: get_gevent_executor,
GreenMode.Asyncio: get_asyncio_executor,
}
__submit_map = {
GreenMode.Synchronous: synch_submit,
GreenMode.Futures: futures_submit,
GreenMode.Gevent: gevent_submit,
GreenMode.Asyncio: asyncio_submit,
}
__wait_map = {
GreenMode.Synchronous: synch_wait,
GreenMode.Futures: futures_wait,
GreenMode.Gevent: gevent_wait,
GreenMode.Asyncio: asyncio_wait,
}
__event_loop_map = {
GreenMode.Synchronous: (lambda: None),
GreenMode.Futures: (lambda: None),
GreenMode.Gevent: get_gevent_event_loop,
GreenMode.Asyncio: get_asyncio_event_loop,
}
# Getters
def get_executor(mode):
return __executor_map[mode]()
def get_submitter(mode):
return __submit_map[mode]
def get_waiter(mode):
return __wait_map[mode]
def get_wait_default_value(mode):
if mode is None:
mode = get_green_mode()
return mode not in (GreenMode.Asyncio,)
# Generic submitter/spawner and waiter
def submit(mode, fn, *args, **kwargs):
return get_submitter(mode)(fn, *args, **kwargs)
spawn = submit
def wait_result(ret, green_mode=None, timeout=None):
if green_mode is None:
green_mode = get_green_mode()
return get_waiter(green_mode)(ret, timeout=timeout)
wait = wait_result
def result(ret, green_mode=None, wait=True, timeout=None):
if not wait:
return ret
return wait_result(ret, green_mode=green_mode, timeout=timeout)
def get_event_loop(mode):
return __event_loop_map[mode]()
# Get object submitter, waiter and green_mode
def get_object_green_mode(obj):
if hasattr(obj, "get_green_mode"):
return obj.get_green_mode()
return get_green_mode()
def get_object_submitter(obj, green_mode=None):
"""Returns the proper submit callable for the given object.
If the object has *_executors* and *_green_mode* members it returns
the submit callable for the executor corresponding to the green_mode.
Otherwise it returns the global submit callable for the given green_mode.
:returns: submit callable"""
# Get green mode
if green_mode is None:
green_mode = get_object_green_mode(obj)
# Get executor
executors = getattr(obj, "_executors", {})
executor = executors.get(green_mode, None)
if executor is None:
executor = get_executor(green_mode)
# Get submitter
if green_mode == GreenMode.Gevent:
return executor.spawn
return executor.submit
def get_object_waiter(obj, green_mode=None):
"""Returns the proper wait callable for the given object.
If the object has *_executors* and *_green_mode* members it returns
the wait callable for the executor corresponding to the green_mode.
Otherwise it returns the global wait callable for the given green_mode.
:returns: wait callable"""
# Get green mode
if green_mode is None:
green_mode = get_object_green_mode(obj)
# Get waiter
waiter = get_waiter(green_mode)
# Asyncio corner case
if green_mode == GreenMode.Asyncio:
executors = getattr(obj, "_executors", {})
executor = executors.get(GreenMode.Asyncio)
if executor is None:
return waiter
return partial(waiter, loop=executor.loop)
# Return waiter
return waiter
# Green decorators
def green(fn):
"""make a method green. Can be used as a decorator"""
@wraps(fn)
def greener(self, *args, **kwargs):
# first take out all green parameters
green_mode = kwargs.pop('green_mode', get_object_green_mode(self))
wait = kwargs.pop('wait', get_wait_default_value(green_mode))
timeout = kwargs.pop('timeout', None)
# get the proper submitable for the given green_mode
submitter = get_object_submitter(self, green_mode)
waiter = get_object_waiter(self, green_mode)
# submit the method
ret = submitter(fn, self, *args, **kwargs)
# return the proper result
return waiter(ret, timeout=timeout) if wait else ret
return greener
def green_cb(fn, green_mode=None):
"""return a green verion of the given callback."""
event_loop = get_event_loop(green_mode)
if event_loop is None:
return fn
@wraps(fn)
def greener(*args, **kwargs):
event_loop.submit(fn, *args, **kwargs)
return greener
|