- 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('Reading %s.', in_file)
+ try:
+ with open(in_file, 'rb') as rb:
+ serialized = rb.read()
+ except Exception as e:
+ logger.exception(e)
+ logger.critical('Problem reading %s. Aborting.', in_file)
+ cleanup_and_exit(thread, stop_thread, 1)
+
+ logger.debug('Deserializing %s', in_file)
+ try:
+ fun, args, kwargs = cloudpickle.loads(serialized)
+ except Exception as e:
+ logger.exception(e)
+ logger.critical('Problem deserializing %s. Aborting.', in_file)
+ cleanup_and_exit(thread, stop_thread, 2)
+
+ logger.debug('Invoking user code...')
+ with Timer() as t:
+ ret = fun(*args, **kwargs)
+ logger.debug('User code took %.1fs', t())
+
+ logger.debug('Serializing results')
+ try:
+ serialized = cloudpickle.dumps(ret)
+ except Exception as e:
+ logger.exception(e)
+ logger.critical('Could not serialize result (%s). Aborting.', type(ret))
+ cleanup_and_exit(thread, stop_thread, 3)
+
+ logger.debug('Writing %s', out_file)
+ try:
+ with open(out_file, 'wb') as wb:
+ wb.write(serialized)
+ except Exception as e:
+ logger.exception(e)
+ logger.critical('Error writing %s. Aborting.', out_file)
+ cleanup_and_exit(thread, stop_thread, 4)
+ cleanup_and_exit(thread, stop_thread, 0)