3 """A simple utility to unpickle some code, run it, and pickle the
14 import cloudpickle # type: ignore
15 import psutil # type: ignore
20 from thread_utils import background_thread
23 logger = logging.getLogger(__file__)
25 cfg = config.add_commandline_args(
26 f"Remote Worker ({__file__})",
27 "Helper to run pickled code remotely and return results",
34 help='The location of the bundle of code to execute.'
41 help='The location where we should write the computation results.'
45 action=argparse_utils.ActionNoYes,
47 help='Should we watch for the cancellation of our parent ssh process?'
52 def watch_for_cancel(terminate_event: threading.Event) -> None:
53 logger.debug('Starting up background thread...')
54 p = psutil.Process(os.getpid())
57 ancestors = p.parents()
58 for ancestor in ancestors:
59 name = ancestor.name()
61 logger.debug(f'Ancestor process {name} (pid={pid})')
62 if 'ssh' in name.lower():
66 logger.error('Did not see sshd in our ancestors list?! Committing suicide.')
68 os.kill(os.getpid(), signal.SIGTERM)
70 os.kill(os.getpid(), signal.SIGKILL)
72 if terminate_event.is_set():
79 in_file = config.config['code_file']
80 out_file = config.config['result_file']
83 if config.config['watch_for_cancel']:
84 (thread, stop_thread) = watch_for_cancel()
86 logger.debug(f'Reading {in_file}.')
88 with open(in_file, 'rb') as rb:
89 serialized = rb.read()
90 except Exception as e:
92 logger.critical(f'Problem reading {in_file}. Aborting.')
96 logger.debug(f'Deserializing {in_file}.')
98 fun, args, kwargs = cloudpickle.loads(serialized)
99 except Exception as e:
101 logger.critical(f'Problem deserializing {in_file}. Aborting.')
105 logger.debug('Invoking user code...')
107 ret = fun(*args, **kwargs)
109 logger.debug(f'User code took {end - start:.1f}s')
111 logger.debug('Serializing results')
113 serialized = cloudpickle.dumps(ret)
114 except Exception as e:
116 logger.critical(f'Could not serialize result ({type(ret)}). Aborting.')
120 logger.debug(f'Writing {out_file}.')
122 with open(out_file, 'wb') as wb:
124 except Exception as e:
126 logger.critical(f'Error writing {out_file}. Aborting.')
130 if stop_thread is not None:
135 if __name__ == '__main__':