|
| 1 | +# |
| 2 | +# This file is protected by Copyright. Please refer to the COPYRIGHT file |
| 3 | +# distributed with this source distribution. |
| 4 | +# |
| 5 | +# This file is part of REDHAWK rtl-demo-app. |
| 6 | +# |
| 7 | +# REDHAWK rtl-demo-app is free software: you can redistribute it and/or modify it under |
| 8 | +# the terms of the GNU Lesser General Public License as published by the Free |
| 9 | +# Software Foundation, either version 3 of the License, or (at your option) any |
| 10 | +# later version. |
| 11 | +# |
| 12 | +# REDHAWK rtl-demo-app is distributed in the hope that it will be useful, but WITHOUT |
| 13 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 | +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 15 | +# details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public License |
| 18 | +# along with this program. If not, see http://www.gnu.org/licenses/. |
| 19 | +# |
| 20 | +from functools import wraps, partial |
| 21 | +from tornado import gen, concurrent |
| 22 | +from tornado import ioloop |
| 23 | + |
| 24 | +# Suppressed known DeprecationWarning for the futures backport |
| 25 | +import warnings, exceptions |
| 26 | +warnings.filterwarnings("ignore", "The futures package has been deprecated.*", exceptions.DeprecationWarning, "futures") |
| 27 | +import futures |
| 28 | + |
| 29 | +import logging |
| 30 | +import sys |
| 31 | +from futures import ThreadPoolExecutor |
| 32 | + |
| 33 | +EXECUTOR = ThreadPoolExecutor(100) |
| 34 | + |
| 35 | +_LINE = '%'*40 |
| 36 | + |
| 37 | + |
| 38 | +def safe_return_future(func): |
| 39 | + ''' |
| 40 | + Identical to tornado.gen.return_future plus |
| 41 | + thread safety. Executes the callback in |
| 42 | + the ioloop thread |
| 43 | + ''' |
| 44 | + @wraps(func) |
| 45 | + def exec_func(*args, **kwargs): |
| 46 | + |
| 47 | + future = concurrent.TracebackFuture() |
| 48 | + |
| 49 | + # accept optional callback |
| 50 | + callback = kwargs.pop('callback', None) |
| 51 | + if callback: |
| 52 | + future.add_done_callback(callback) |
| 53 | + |
| 54 | + try: |
| 55 | + |
| 56 | + io_loop = kwargs.pop('ioloop', None) |
| 57 | + if not io_loop: |
| 58 | + io_loop = ioloop.IOLoop.current() |
| 59 | + |
| 60 | + def _ioloop_callback(val): |
| 61 | + # print "set result to %s" % val |
| 62 | + future.set_result(val) |
| 63 | + |
| 64 | + def _callback(val): |
| 65 | + # set the result in the ioloop thread |
| 66 | + # print "callback val is %s " % val |
| 67 | + io_loop.add_callback(_ioloop_callback, val) |
| 68 | + |
| 69 | + # print "Func %s " % func |
| 70 | + func(callback=_callback, *args, **kwargs) |
| 71 | + except Exception: |
| 72 | + future.set_exc_info(sys.exc_info()) |
| 73 | + |
| 74 | + return future |
| 75 | + |
| 76 | + exec_func.__doc__ = \ |
| 77 | + ("%s\nsafe_return_future() wrapped function.\n" + |
| 78 | + "Runs asynchronously and returns a Future.\n" + |
| 79 | + "See _utils.concurrent for more info\n%s\n%s") % (_LINE, _LINE, exec_func.__doc__) |
| 80 | + return exec_func |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +def background_task(func): |
| 85 | + |
| 86 | + @wraps(func) |
| 87 | + def exec_background(*args, **kwargs): |
| 88 | + ''' |
| 89 | + Executes a function in a background thread |
| 90 | + and returns a Future invoked when the thread completes. |
| 91 | + Useful for IO Bound processes that block. For CPU |
| 92 | + bound processes consider using celery, DO NOT execute |
| 93 | + CPU Bound tasks in the tornado process! |
| 94 | +
|
| 95 | + io_loop is the optional ioloop used to invoke the callback |
| 96 | + in the processing thread. This is useful for unit tests |
| 97 | + that do not use the singleton ioloop. If set to none, |
| 98 | + IOLoop.current() is returned |
| 99 | + ''' |
| 100 | + # traceback future maintains python stack in exception |
| 101 | + future = concurrent.TracebackFuture() |
| 102 | + |
| 103 | + # use explicit ioloop for unit testing |
| 104 | + # Ref: https://github.com/tornadoweb/tornado/issues/663 |
| 105 | + io_loop = kwargs.pop('ioloop', None) |
| 106 | + if not io_loop: |
| 107 | + io_loop = ioloop.IOLoop.current() |
| 108 | + |
| 109 | + # accept optional callback |
| 110 | + callback = kwargs.pop('callback', None) |
| 111 | + if callback: |
| 112 | + future.add_done_callback(callback) |
| 113 | + |
| 114 | + def _do_task(*args, **kwargs): |
| 115 | + try: |
| 116 | + rtn = func(*args, **kwargs) |
| 117 | + io_loop.add_callback(future.set_result, rtn) |
| 118 | + except Exception, e: |
| 119 | + logging.debug("Callback exception", exc_info=True) |
| 120 | + io_loop.add_callback(future.set_exc_info, sys.exc_info()) |
| 121 | + |
| 122 | + EXECUTOR.submit(partial(_do_task, *args, **kwargs)) |
| 123 | + return future |
| 124 | + |
| 125 | + exec_background.__doc__ = \ |
| 126 | + ("%s\nbackground_task() wrapped function.\n" + |
| 127 | + "Runs asynchronously and returns a Future.\n" + |
| 128 | + "See _utils.concurrent for more info\n%s\n%s") % (_LINE, _LINE, exec_background.__doc__) |
| 129 | + return exec_background |
0 commit comments