2016-09-21 12:21:18 +00:00
|
|
|
from trezor import log, loop
|
|
|
|
|
2016-10-06 10:35:05 +00:00
|
|
|
_started = []
|
|
|
|
_default = None
|
|
|
|
_default_genfunc = None
|
2016-09-21 12:21:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start_default(genfunc):
|
2016-10-06 10:35:05 +00:00
|
|
|
global _default
|
|
|
|
global _default_genfunc
|
|
|
|
_default_genfunc = genfunc
|
|
|
|
_default = _default_genfunc()
|
|
|
|
log.info(__name__, 'start default %s', _default)
|
|
|
|
loop.schedule_task(_default)
|
2016-09-21 12:21:18 +00:00
|
|
|
|
|
|
|
|
2016-10-06 10:35:05 +00:00
|
|
|
def close_default():
|
|
|
|
global _default
|
|
|
|
log.info(__name__, 'close default %s', _default)
|
|
|
|
_default.close()
|
|
|
|
_default = None
|
|
|
|
|
2016-09-21 12:21:18 +00:00
|
|
|
|
2016-12-08 15:18:58 +00:00
|
|
|
def start(workflow):
|
2016-10-06 10:35:05 +00:00
|
|
|
if _default is not None:
|
|
|
|
close_default()
|
|
|
|
_started.append(workflow)
|
|
|
|
log.info(__name__, 'start %s', workflow)
|
2016-12-08 15:18:58 +00:00
|
|
|
loop.schedule_task(_watch(workflow))
|
2016-09-21 12:21:18 +00:00
|
|
|
|
|
|
|
|
2016-12-08 15:18:58 +00:00
|
|
|
async def _watch(workflow):
|
2016-09-21 12:21:18 +00:00
|
|
|
try:
|
|
|
|
return await workflow
|
|
|
|
finally:
|
2016-10-06 10:35:05 +00:00
|
|
|
_started.remove(workflow)
|
|
|
|
if not _started and _default_genfunc is not None:
|
|
|
|
start_default(_default_genfunc)
|