3 # © Copyright 2021-2022, Scott Gasch
5 """A simple utility to unpickle some code, run it, and pickle the
6 results. Please don't unpickle (or run!) code you do not know.
15 from typing import Optional
17 import cloudpickle # type: ignore
18 import psutil # type: ignore
20 from pyutils import argparse_utils, bootstrap, config
21 from pyutils.parallelize.thread_utils import background_thread
22 from pyutils.stopwatch import Timer
24 logger = logging.getLogger(__file__)
26 cfg = config.add_commandline_args(
27 f"Remote Worker ({__file__})",
28 "Helper to run pickled code remotely and return results",
35 help='The location of the bundle of code to execute.',
42 help='The location where we should write the computation results.',
46 action=argparse_utils.ActionNoYes,
48 help='Should we watch for the cancellation of our parent ssh process?',
53 def watch_for_cancel(terminate_event: threading.Event) -> None:
54 logger.debug('Starting up background thread...')
55 p = psutil.Process(os.getpid())
58 ancestors = p.parents()
59 for ancestor in ancestors:
60 name = ancestor.name()
62 logger.debug('Ancestor process %s (pid=%d)', name, pid)
63 if 'ssh' in name.lower():
68 'Did not see sshd in our ancestors list?! Committing suicide.'
71 os.kill(os.getpid(), signal.SIGTERM)
73 os.kill(os.getpid(), signal.SIGKILL)
75 if terminate_event.is_set():
81 thread: Optional[threading.Thread],
82 stop_thread: Optional[threading.Event],
85 if stop_thread is not None:
87 assert thread is not None
94 in_file = config.config['code_file']
95 out_file = config.config['result_file']
99 if config.config['watch_for_cancel']:
100 (thread, stop_thread) = watch_for_cancel()
102 logger.debug('Reading %s.', in_file)
104 with open(in_file, 'rb') as rb:
105 serialized = rb.read()
106 except Exception as e:
108 logger.critical('Problem reading %s. Aborting.', in_file)
109 cleanup_and_exit(thread, stop_thread, 1)
111 logger.debug('Deserializing %s', in_file)
113 fun, args, kwargs = cloudpickle.loads(serialized)
114 except Exception as e:
116 logger.critical('Problem deserializing %s. Aborting.', in_file)
117 cleanup_and_exit(thread, stop_thread, 2)
119 logger.debug('Invoking user code...')
121 ret = fun(*args, **kwargs)
122 logger.debug('User code took %.1fs', t())
124 logger.debug('Serializing results')
126 serialized = cloudpickle.dumps(ret)
127 except Exception as e:
129 logger.critical('Could not serialize result (%s). Aborting.', type(ret))
130 cleanup_and_exit(thread, stop_thread, 3)
132 logger.debug('Writing %s', out_file)
134 with open(out_file, 'wb') as wb:
136 except Exception as e:
138 logger.critical('Error writing %s. Aborting.', out_file)
139 cleanup_and_exit(thread, stop_thread, 4)
140 cleanup_and_exit(thread, stop_thread, 0)
143 if __name__ == '__main__':