| 1 | n/a | import io |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | from .context import reduction, set_spawning_popen |
|---|
| 5 | n/a | if not reduction.HAVE_SEND_HANDLE: |
|---|
| 6 | n/a | raise ImportError('No support for sending fds between processes') |
|---|
| 7 | n/a | from . import forkserver |
|---|
| 8 | n/a | from . import popen_fork |
|---|
| 9 | n/a | from . import spawn |
|---|
| 10 | n/a | from . import util |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | __all__ = ['Popen'] |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | # |
|---|
| 16 | n/a | # Wrapper for an fd used while launching a process |
|---|
| 17 | n/a | # |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | class _DupFd(object): |
|---|
| 20 | n/a | def __init__(self, ind): |
|---|
| 21 | n/a | self.ind = ind |
|---|
| 22 | n/a | def detach(self): |
|---|
| 23 | n/a | return forkserver.get_inherited_fds()[self.ind] |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | # |
|---|
| 26 | n/a | # Start child process using a server process |
|---|
| 27 | n/a | # |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | class Popen(popen_fork.Popen): |
|---|
| 30 | n/a | method = 'forkserver' |
|---|
| 31 | n/a | DupFd = _DupFd |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | def __init__(self, process_obj): |
|---|
| 34 | n/a | self._fds = [] |
|---|
| 35 | n/a | super().__init__(process_obj) |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | def duplicate_for_child(self, fd): |
|---|
| 38 | n/a | self._fds.append(fd) |
|---|
| 39 | n/a | return len(self._fds) - 1 |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | def _launch(self, process_obj): |
|---|
| 42 | n/a | prep_data = spawn.get_preparation_data(process_obj._name) |
|---|
| 43 | n/a | buf = io.BytesIO() |
|---|
| 44 | n/a | set_spawning_popen(self) |
|---|
| 45 | n/a | try: |
|---|
| 46 | n/a | reduction.dump(prep_data, buf) |
|---|
| 47 | n/a | reduction.dump(process_obj, buf) |
|---|
| 48 | n/a | finally: |
|---|
| 49 | n/a | set_spawning_popen(None) |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | self.sentinel, w = forkserver.connect_to_new_process(self._fds) |
|---|
| 52 | n/a | util.Finalize(self, os.close, (self.sentinel,)) |
|---|
| 53 | n/a | with open(w, 'wb', closefd=True) as f: |
|---|
| 54 | n/a | f.write(buf.getbuffer()) |
|---|
| 55 | n/a | self.pid = forkserver.read_unsigned(self.sentinel) |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def poll(self, flag=os.WNOHANG): |
|---|
| 58 | n/a | if self.returncode is None: |
|---|
| 59 | n/a | from multiprocessing.connection import wait |
|---|
| 60 | n/a | timeout = 0 if flag == os.WNOHANG else None |
|---|
| 61 | n/a | if not wait([self.sentinel], timeout): |
|---|
| 62 | n/a | return None |
|---|
| 63 | n/a | try: |
|---|
| 64 | n/a | self.returncode = forkserver.read_unsigned(self.sentinel) |
|---|
| 65 | n/a | except (OSError, EOFError): |
|---|
| 66 | n/a | # The process ended abnormally perhaps because of a signal |
|---|
| 67 | n/a | self.returncode = 255 |
|---|
| 68 | n/a | return self.returncode |
|---|