Open a file for writing, then block such that the client will hold a capability. Don't return until the remote process has got as far as opening the file, then return the RemoteProcess instance.
(self, basename="background_file", write=True, content="content")
| 985 | p.wait() |
| 986 | |
| 987 | def open_background(self, basename="background_file", write=True, content="content"): |
| 988 | """ |
| 989 | Open a file for writing, then block such that the client |
| 990 | will hold a capability. |
| 991 | |
| 992 | Don't return until the remote process has got as far as opening |
| 993 | the file, then return the RemoteProcess instance. |
| 994 | """ |
| 995 | assert(self.is_mounted()) |
| 996 | |
| 997 | path = os.path.join(self.hostfs_mntpt, basename) |
| 998 | |
| 999 | if write: |
| 1000 | pyscript = dedent(""" |
| 1001 | import fcntl |
| 1002 | import os |
| 1003 | import sys |
| 1004 | import time |
| 1005 | |
| 1006 | fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) |
| 1007 | |
| 1008 | with open("{path}", 'w') as f: |
| 1009 | f.write("{content}") |
| 1010 | f.flush() |
| 1011 | while True: |
| 1012 | print("open_background: keeping file open", file=sys.stderr) |
| 1013 | try: |
| 1014 | if os.read(0, 4096) == b"": |
| 1015 | break |
| 1016 | except BlockingIOError: |
| 1017 | pass |
| 1018 | time.sleep(2) |
| 1019 | """).format(path=path, content=content) |
| 1020 | else: |
| 1021 | pyscript = dedent(""" |
| 1022 | import fcntl |
| 1023 | import os |
| 1024 | import sys |
| 1025 | import time |
| 1026 | |
| 1027 | fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) |
| 1028 | |
| 1029 | with open("{path}", 'r') as f: |
| 1030 | while True: |
| 1031 | print("open_background: keeping file open", file=sys.stderr) |
| 1032 | try: |
| 1033 | if os.read(0, 4096) == b"": |
| 1034 | break |
| 1035 | except BlockingIOError: |
| 1036 | pass |
| 1037 | time.sleep(2) |
| 1038 | """).format(path=path) |
| 1039 | |
| 1040 | rproc = self._run_python(pyscript) |
| 1041 | self.background_procs.append(rproc) |
| 1042 | |
| 1043 | # This wait would not be sufficient if the file had already |
| 1044 | # existed, but it's simple and in practice users of open_background |