- with open(in_file, 'rb') as rb:
- serialized = rb.read()
-
- fun, args, kwargs = cloudpickle.loads(serialized)
- ret = fun(*args, **kwargs)
-
- serialized = cloudpickle.dumps(ret)
- with open(out_file, 'wb') as wb:
- wb.write(serialized)
-
- # Windows-Linux is retarded.
- if hostname != 'VIDEO-COMPUTER':
- terminate_event.set()
- thread.join()
- sys.exit(0)
+ thread = None
+ stop_thread = None
+ if config.config['watch_for_cancel']:
+ (thread, stop_thread) = watch_for_cancel()
+
+ logger.debug(f'Reading {in_file}.')
+ try:
+ with open(in_file, 'rb') as rb:
+ serialized = rb.read()
+ except Exception as e:
+ logger.exception(e)
+ logger.critical(f'Problem reading {in_file}. Aborting.')
+ cleanup_and_exit(thread, stop_thread, 1)
+
+ logger.debug(f'Deserializing {in_file}.')
+ try:
+ fun, args, kwargs = cloudpickle.loads(serialized)
+ except Exception as e:
+ logger.exception(e)
+ logger.critical(f'Problem deserializing {in_file}. Aborting.')
+ cleanup_and_exit(thread, stop_thread, 2)
+
+ logger.debug('Invoking user code...')
+ with Timer() as t:
+ ret = fun(*args, **kwargs)
+ logger.debug(f'User code took {t():.1f}s')
+
+ logger.debug('Serializing results')
+ try:
+ serialized = cloudpickle.dumps(ret)
+ except Exception as e:
+ logger.exception(e)
+ logger.critical(f'Could not serialize result ({type(ret)}). Aborting.')
+ cleanup_and_exit(thread, stop_thread, 3)
+
+ logger.debug(f'Writing {out_file}.')
+ try:
+ with open(out_file, 'wb') as wb:
+ wb.write(serialized)
+ except Exception as e:
+ logger.exception(e)
+ logger.critical(f'Error writing {out_file}. Aborting.')
+ cleanup_and_exit(thread, stop_thread, 4)
+ cleanup_and_exit(thread, stop_thread, 0)