本文整理汇总了Python中twisted.internet.defer.inlineCallbacks函数的典型用法代码示例。如果您正苦于以下问题:Python inlineCallbacks函数的具体用法?Python inlineCallbacks怎么用?Python inlineCallbacks使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了inlineCallbacks函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_inlineCallbacksTracebacks
def test_inlineCallbacksTracebacks(self):
"""
inlineCallbacks that re-raise tracebacks into their deferred
should not lose their tracebacsk.
"""
f = getDivisionFailure()
d = defer.Deferred()
try:
f.raiseException()
except:
d.errback()
failures = []
def collect_error(result):
failures.append(result)
def ic(d):
yield d
ic = defer.inlineCallbacks(ic)
ic(d).addErrback(collect_error)
newFailure, = failures
self.assertEquals(
traceback.extract_tb(newFailure.getTracebackObject())[-1][-1],
"1/0"
)
示例2: __init__
def __init__(self, orig, max_entries=1000, num_args=1, lru=True,
inlineCallbacks=False):
self.orig = orig
if inlineCallbacks:
self.function_to_call = defer.inlineCallbacks(orig)
else:
self.function_to_call = orig
self.max_entries = max_entries
self.num_args = num_args
self.lru = lru
self.arg_names = inspect.getargspec(orig).args[1:num_args+1]
if len(self.arg_names) < self.num_args:
raise Exception(
"Not enough explicit positional arguments to key off of for %r."
" (@cached cannot key off of *args or **kwars)"
% (orig.__name__,)
)
self.cache = Cache(
name=self.orig.__name__,
max_entries=self.max_entries,
keylen=self.num_args,
lru=self.lru,
)
示例3: loadCommands
def loadCommands(self):
commands = {}
path = yield self.config.get("path","commands")
for loader, name, ispkg in pkgutil.iter_modules([path]):
if ispkg:
continue
try:
command = getattr(__import__(path, fromlist=[name.encode("utf8")]), name)
reload(command)
command.config["name"] = name
command.config["command"] = inlineCallbacks(command.command) if inspect.isgeneratorfunction(command.command) else command.command
args, _, _, kwargs = inspect.getargspec(command.command)
if args[:5] != ["guid", "manager", "irc", "channel", "user"]:
continue
if kwargs:
boundary = -1 * len(kwargs)
command.config["args"] = args[5:boundary]
command.config["kwargs"] = args[boundary:]
else:
command.config["args"] = args[5:]
command.config["kwargs"] = []
if "disabled" in command.config and command.config["disabled"]:
continue
commands[name] = command.config
except:
self.err("Failed to load {}.{}", path, name)
self.commands = commands
示例4: operation
def operation(func):
func = defer.inlineCallbacks(func)
def wrap(self, *a, **kw):
return self._do_operation(func, self, *a, **kw)
return wrap
示例5: delayed
def delayed(func):
func = inlineCallbacks(func)
@wraps(func)
def wrapper(self, request, *args, **kwargs):
was_interrupted = [False]
log = Logger('web')
def on_cancel(failure):
err(failure, 'Call to "%s" was interrupted' % request.path)
was_interrupted[0] = True
request.notifyFinish().addErrback(on_cancel)
def finish_request(result):
log.debug('%s(%r, args=%r, kwargs=%r)=%s' % (func.__name__, request, args, kwargs, result))
if isinstance(result, Failure):
request.setResponseCode(INTERNAL_SERVER_ERROR)
log.exception('Call to %s(%r, args=%r, kwargs=%r) failed' % (func.__name__, request, args, kwargs), exc_info = (result.type, result.value, result.getTracebackObject()))
if was_interrupted[0] == False and result != NOT_DONE_YET:
request.finish()
log.debug('Calling %s(%r, args=%r, kwargs=%r)' % (func.__name__, request, args, kwargs))
d = func(self, request, *args, **kwargs)
log.debug('Result: %s' % d)
log.debug('is returned deferred was called? %s' % d.called)
d.addBoth(finish_request)
return NOT_DONE_YET
return wrapper
示例6: action
def action(fn):
"""
Decorator which wraps an action callback conforming to the twisted
inlineCallbacks interface. This lets us write synchronous-looking code
that uses asynchronous methods from twisted.
"""
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from twisted.python.failure import Failure
inlinefn = inlineCallbacks(fn)
def on_return(ret):
if isinstance(ret, Failure):
print "action failed: " + ret.getErrorMessage()
reactor.stop()
def trampoline(ns):
section = ns.get_section('client')
debug = section.get_bool("debug", False)
if debug:
startLogging(StdoutHandler(), DEBUG)
else:
startLogging(None)
def when_running():
defer = inlinefn(ns)
defer.addBoth(on_return)
reactor.callWhenRunning(when_running)
reactor.run()
return trampoline
示例7: wrapDatabaseAndCallbacks
def wrapDatabaseAndCallbacks(fn):
"""
Decorator; convenience for methods that need mock db and yield-Deferred syntax
"""
fnICB = defer.inlineCallbacks(fn)
fnMockedConfig = mockConfig()(fnICB)
return wraps(fn)(fnMockedConfig)
示例8: wrap
def wrap(fn):
if inspect.isgeneratorfunction(fn):
fn = inlineCallbacks(fn)
@functools.wraps(fn)
@twistedtools.deferred(timeout=fn.timeout if hasattr(fn, 'timeout') else 1.0)
def ret():
# dbg("\n============================================\n")
import spinoff.actor._actor
spinoff.actor._actor.TESTING = True
Actor.reset_flags(debug=True)
# TODO: once the above TODO (fresh Node for each test fn) is complete, consider making Events non-global by
# having each Node have its own Events instance.
Events.reset()
def check_memleaks():
if '__pypy__' not in sys.builtin_module_names:
gc.collect()
for trash in gc.garbage[:]:
if isinstance(trash, DebugInfo):
# dbg("DEBUGINFO: __del__")
if trash.failResult is not None:
exc = Unclean(repr(trash.failResult.value) + '\n' + str(trash._getDebugTracebacks()))
trash.__dict__.clear()
raise exc
gc.garbage.remove(trash)
assert not gc.garbage, "Memory leak detected: %r" % (gc.garbage,)
# if gc.garbage:
# dbg("GARGABE: detected after %s:" % (fn.__name__,), len(gc.garbage))
# import objgraph as ob
# import os
# def dump_chain(g_):
# def calling_test(x):
# if not isframework(x):
# return None
# import spinoff
# isframework = lambda x: type(x).__module__.startswith(spinoff.__name__)
# ob.show_backrefs([g_], filename='backrefs.png', max_depth=100, highlight=isframework)
# for gen in gc.garbage:
# dump_chain(gen)
# dbg(" TESTWRAP: mem-debuggin", gen)
# import pdb; pdb.set_trace()
# os.remove('backrefs.png')
return (
deferred_with(ErrorCollector(), fn)
.addBoth(lambda result: Node.stop_all().addCallback(lambda _: result))
.addBoth(lambda result: (_process_idle_calls(), result)[-1])
.addBoth(lambda result: (check_memleaks(), result)[-1])
)
return ret
示例9: testReturnNoValue
def testReturnNoValue(self):
"""Ensure a standard python return results in a None result."""
def _noReturn():
yield 5
return
_noReturn = inlineCallbacks(_noReturn)
return _noReturn().addCallback(self.assertEqual, None)
示例10: testReturnValue
def testReturnValue(self):
"""Ensure that returnValue works."""
def _return():
yield 5
returnValue(6)
_return = inlineCallbacks(_return)
return _return().addCallback(self.assertEqual, 6)
示例11: set_route
def set_route(call, method, route, **kwargs):
call = defer.inlineCallbacks(call)
call.method = method
call.route = re.compile(route)
call.template = kwargs.get('template', None)
call.last = kwargs.get('last', False)
call.redirect = kwargs.get('redirect', None)
return call
示例12: deferredPage
def deferredPage(func):
func = defer.inlineCallbacks(func)
def wrap(self, request):
(func(self, request)
.addErrback(request.processingFailed)
.addErrback(lambda f: None))
return NOT_DONE_YET
return wrap
示例13: webInlineCallbacks
def webInlineCallbacks(function):
function = inlineCallbacks(function)
def __func__(*args, **kwargs):
function(*args, **kwargs)
return NOT_DONE_YET
return __func__
示例14: async_test
def async_test(f, *args, **kwargs):
"Starts an asynchronous test, waits for it to complete, and returns its result."
result = []
def cb(value, good):
result.append(good)
result.append(value)
inlineCallbacks(f)(*args, **kwargs).addCallbacks(callback = cb, callbackArgs = [True],
errback = cb, errbackArgs = [False])
while not result:
reactor.iterate(0.02)
if result[0]:
# Uncomment the following line to check that all the tests
# really are being run to completion.
#raise(Exception("Success"))
return result[1]
else:
result[1].printTraceback()
result[1].raiseException()
示例15: __init__
def __init__(self, orig, num_args, inlineCallbacks, cache_context=False):
self.orig = orig
if inlineCallbacks:
self.function_to_call = defer.inlineCallbacks(orig)
else:
self.function_to_call = orig
arg_spec = inspect.getargspec(orig)
all_args = arg_spec.args
if "cache_context" in all_args:
if not cache_context:
raise ValueError(
"Cannot have a 'cache_context' arg without setting"
" cache_context=True"
)
elif cache_context:
raise ValueError(
"Cannot have cache_context=True without having an arg"
" named `cache_context`"
)
if num_args is None:
num_args = len(all_args) - 1
if cache_context:
num_args -= 1
if len(all_args) < num_args + 1:
raise Exception(
"Not enough explicit positional arguments to key off for %r: "
"got %i args, but wanted %i. (@cached cannot key off *args or "
"**kwargs)"
% (orig.__name__, len(all_args), num_args)
)
self.num_args = num_args
# list of the names of the args used as the cache key
self.arg_names = all_args[1:num_args + 1]
# self.arg_defaults is a map of arg name to its default value for each
# argument that has a default value
if arg_spec.defaults:
self.arg_defaults = dict(zip(
all_args[-len(arg_spec.defaults):],
arg_spec.defaults
))
else:
self.arg_defaults = {}
if "cache_context" in self.arg_names:
raise Exception(
"cache_context arg cannot be included among the cache keys"
)
self.add_cache_context = cache_context