/usr/lib/python3/dist-packages/PyTango/tango_gevent.py is in python3-pytango 8.1.8-1.
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 | # ------------------------------------------------------------------------------
# This file is part of PyTango (http://www.tinyurl.com/PyTango)
#
# 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.
# ------------------------------------------------------------------------------
from __future__ import absolute_import
import sys
import types
__all__ = ["get_global_threadpool", "get_global_executor",
"get_event_loop", "submit", "spawn"]
def get_global_threadpool():
import gevent
return gevent.get_hub().threadpool
class ExceptionWrapper:
def __init__(self, exception, error_string, tb):
self.exception = exception
self.error_string = error_string
self.tb = tb
class wrap_errors(object):
def __init__(self, func):
"""Make a new function from `func', such that it catches all exceptions
and return it as a specific object
"""
self.func = func
def __call__(self, *args, **kwargs):
func = self.func
try:
return func(*args, **kwargs)
except:
return ExceptionWrapper(*sys.exc_info())
def __str__(self):
return str(self.func)
def __repr__(self):
return repr(self.func)
def __getattr__(self, item):
return getattr(self.func, item)
def get_with_exception(g, block=True, timeout=None):
result = g._get(block, timeout)
if isinstance(result, ExceptionWrapper):
# raise the exception using the caller context
raise result.error_string, None, result.tb
else:
return result
def spawn(fn, *args, **kwargs):
# the gevent threadpool do not raise exception with asyncresults, we have to wrap it
fn = wrap_errors(fn)
g = get_global_threadpool().spawn(fn, *args, **kwargs)
g._get = g.get
g.get = types.MethodType(get_with_exception, g)
return g
get_global_executor = get_global_threadpool
submit = spawn
__event_loop = None
def get_event_loop():
global __event_loop
if __event_loop is None:
import gevent
import gevent.queue
def loop(queue):
while True:
item = queue.get()
try:
f, args, kwargs = item
gevent.spawn(f, *args, **kwargs)
except Exception as e:
sys.excepthook(*sys.exc_info())
def submit(fn, *args, **kwargs):
l_async = queue.hub.loop.async()
queue.put((fn, args, kwargs))
l_async.send()
queue = gevent.queue.Queue()
__event_loop = gevent.spawn(loop, queue)
__event_loop.submit = submit
return __event_loop
|