Open a file for writing, complete as soon as you can :param basename: :return:
(self, basename="background_file", loop=False)
| 1223 | ]) |
| 1224 | |
| 1225 | def write_background(self, basename="background_file", loop=False): |
| 1226 | """ |
| 1227 | Open a file for writing, complete as soon as you can |
| 1228 | :param basename: |
| 1229 | :return: |
| 1230 | """ |
| 1231 | assert(self.is_mounted()) |
| 1232 | |
| 1233 | path = os.path.join(self.hostfs_mntpt, basename) |
| 1234 | |
| 1235 | pyscript = dedent(""" |
| 1236 | import fcntl |
| 1237 | import os |
| 1238 | import sys |
| 1239 | import time |
| 1240 | |
| 1241 | fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) |
| 1242 | |
| 1243 | fd = os.open("{path}", os.O_RDWR | os.O_CREAT, 0o644) |
| 1244 | try: |
| 1245 | while True: |
| 1246 | print("write_background: writing", file=sys.stderr) |
| 1247 | os.write(fd, b'content') |
| 1248 | if not {loop}: |
| 1249 | break |
| 1250 | try: |
| 1251 | if os.read(0, 4096) == b"": |
| 1252 | break |
| 1253 | except BlockingIOError: |
| 1254 | pass |
| 1255 | time.sleep(2) |
| 1256 | except IOError as e: |
| 1257 | pass |
| 1258 | os.close(fd) |
| 1259 | """).format(path=path, loop=str(loop)) |
| 1260 | |
| 1261 | rproc = self._run_python(pyscript) |
| 1262 | self.background_procs.append(rproc) |
| 1263 | return rproc |
| 1264 | |
| 1265 | def write_n_mb(self, filename, n_mb, seek=0, wait=True): |
| 1266 | """ |