本文整理汇总了Python中freenas.dispatcher.client.Client.call_task_sync方法的典型用法代码示例。如果您正苦于以下问题:Python Client.call_task_sync方法的具体用法?Python Client.call_task_sync怎么用?Python Client.call_task_sync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freenas.dispatcher.client.Client
的用法示例。
在下文中一共展示了Client.call_task_sync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Context
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import call_task_sync [as 别名]
#.........这里部分代码省略.........
def connection_error(self, event, **kwargs):
if event == ClientError.LOGOUT:
output_msg('Logged out from server.')
self.connection.disconnect()
sys.exit(0)
if event == ClientError.CONNECTION_CLOSED:
time.sleep(1)
self.__try_reconnect()
return
def handle_event(self, event, data):
if event == 'task.updated':
if data['id'] in self.task_callbacks:
self.handle_task_callback(data)
self.print_event(event, data)
def handle_task_callback(self, data):
if data['state'] in ('FINISHED', 'CANCELLED', 'ABORTED', 'FAILED'):
self.task_callbacks[data['id']](data['state'])
def print_event(self, event, data):
if self.event_divert:
self.event_queue.put((event, data))
return
if event == 'task.progress':
return
output_lock.acquire()
self.ml.blank_readline()
translation = events.translate(self, event, data)
if translation:
output_msg(translation)
if 'state' in data:
if data['state'] == 'FAILED':
status = self.connection.call_sync('task.status', data['id'])
output_msg(_(
"Task #{0} error: {1}".format(
data['id'],
status['error'].get('message', '') if status.get('error') else ''
)
))
sys.stdout.flush()
self.ml.restore_readline()
output_lock.release()
def call_sync(self, name, *args, **kwargs):
return wrap(self.connection.call_sync(name, *args, **kwargs))
def call_task_sync(self, name, *args, **kwargs):
self.ml.skip_prompt_print = True
wrapped_result = wrap(self.connection.call_task_sync(name, *args))
self.ml.skip_prompt_print = False
return wrapped_result
def submit_task(self, name, *args, **kwargs):
callback = kwargs.pop('callback', None)
message_formatter = kwargs.pop('message_formatter', None)
if not self.variables.get('tasks_blocking'):
tid = self.connection.call_sync('task.submit', name, args)
if callback:
self.task_callbacks[tid] = callback
return tid
else:
output_msg(_("Hit Ctrl+C to terminate task if needed"))
self.event_divert = True
tid = self.connection.call_sync('task.submit', name, args)
progress = ProgressBar()
try:
while True:
event, data = self.event_queue.get()
if event == 'task.progress' and data['id'] == tid:
message = data['message']
if isinstance(message_formatter, collections.Callable):
message = message_formatter(message)
progress.update(percentage=data['percentage'], message=message)
if event == 'task.updated' and data['id'] == tid:
progress.update(message=data['state'])
if data['state'] == 'FINISHED':
progress.finish()
break
if data['state'] == 'FAILED':
print()
break
except KeyboardInterrupt:
print()
output_msg(_("User requested task termination. Task abort signal sent"))
self.call_sync('task.abort', tid)
self.event_divert = False
return tid