3 """A simple utility to unpickle some code, run it, and pickle the
14 import cloudpickle # type: ignore
15 import psutil # type: ignore
19 from thread_utils import background_thread
22 cfg = config.add_commandline_args(
23 f"Remote Worker ({__file__})",
24 "Helper to run pickled code remotely and return results",
31 help='The location of the bundle of code to execute.'
38 help='The location where we should write the computation results.'
43 def watch_for_cancel(terminate_event: threading.Event) -> None:
44 p = psutil.Process(os.getpid())
47 ancestors = p.parents()
48 for ancestor in ancestors:
49 name = ancestor.name()
50 if 'ssh' in name or 'Ssh' in name:
56 os.kill(os.getpid(), signal.SIGTERM)
57 if terminate_event.is_set():
64 hostname = platform.node()
66 # Windows-Linux is retarded.
67 if hostname != 'VIDEO-COMPUTER':
68 (thread, terminate_event) = watch_for_cancel()
70 in_file = config.config['code_file']
71 out_file = config.config['result_file']
73 with open(in_file, 'rb') as rb:
74 serialized = rb.read()
76 fun, args, kwargs = cloudpickle.loads(serialized)
77 ret = fun(*args, **kwargs)
79 serialized = cloudpickle.dumps(ret)
80 with open(out_file, 'wb') as wb:
83 # Windows-Linux is retarded.
84 if hostname != 'VIDEO-COMPUTER':
90 if __name__ == '__main__':