本文整理汇总了Python中twisted.internet.defer.Deferred.addActionFinish方法的典型用法代码示例。如果您正苦于以下问题:Python Deferred.addActionFinish方法的具体用法?Python Deferred.addActionFinish怎么用?Python Deferred.addActionFinish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.defer.Deferred
的用法示例。
在下文中一共展示了Deferred.addActionFinish方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from twisted.internet.defer import Deferred [as 别名]
# 或者: from twisted.internet.defer.Deferred import addActionFinish [as 别名]
def run(reactor, command, **kwargs):
"""
Run a process and kill it if the reactor stops.
:param reactor: Reactor to use.
:param list command: The command to run.
:return Deferred: Deferred that fires when the process is ended.
"""
if 'env' not in kwargs:
kwargs['env'] = os.environ
action = RUN_ACTION(command=command)
protocol_done = Deferred()
protocol = CommandProtocol(deferred=protocol_done, action=action)
with action.context():
protocol_done = DeferredContext(protocol_done)
reactor.spawnProcess(protocol, command[0], command, **kwargs)
def unregister_killer(result, trigger_id):
try:
reactor.removeSystemEventTrigger(trigger_id)
except:
# If we can't remove the trigger, presumably it has already
# been removed (or run). In any case, there is nothing sensible
# to do if this fails.
pass
return result
trigger_id = reactor.addSystemEventTrigger(
'before', 'shutdown', protocol.transport.signalProcess, 'TERM')
protocol_done.addBoth(unregister_killer, trigger_id)
return protocol_done.addActionFinish()
示例2: run
# 需要导入模块: from twisted.internet.defer import Deferred [as 别名]
# 或者: from twisted.internet.defer.Deferred import addActionFinish [as 别名]
def run(reactor, command, handle_stdout=None, handle_stderr=None, **kwargs):
"""
Run a process and kill it if the reactor stops.
:param reactor: Reactor to use.
:param list command: The command to run.
:param handle_stdout: Callable that will be called with lines parsed
from the command stdout. By default logs an Eliot message.
:param handle_stderr: Callable that will be called with lines parsed
from the command stderr. By default logs an Eliot message.
:return Deferred: Deferred that fires when the process is ended.
"""
if 'env' not in kwargs:
kwargs['env'] = os.environ
action = RUN_ACTION(command=command)
if handle_stdout is None:
def handle_stdout(line):
RUN_OUTPUT_MESSAGE(
line=line,
).write(action=action)
if handle_stderr is None:
def handle_stderr(line):
RUN_ERROR_MESSAGE(
line=line,
).write(action=action)
protocol_done = Deferred()
protocol = CommandProtocol(
deferred=protocol_done,
handle_stdout=handle_stdout,
handle_stderr=handle_stderr,
)
with action.context():
protocol_done = DeferredContext(protocol_done)
reactor.spawnProcess(protocol, command[0], command, **kwargs)
def unregister_killer(result, trigger_id):
try:
reactor.removeSystemEventTrigger(trigger_id)
except:
# If we can't remove the trigger, presumably it has already
# been removed (or run). In any case, there is nothing sensible
# to do if this fails.
pass
return result
trigger_id = reactor.addSystemEventTrigger(
'before', 'shutdown', protocol.transport.signalProcess, 'TERM')
protocol_done.addBoth(unregister_killer, trigger_id)
return protocol_done.addActionFinish()