runtime/spawn_subprocess.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 11 Mar 2019 01:03:32 +0100
changeset 2492 7dd551ac2fa0
parent 2322 7ce4e5cf6339
child 2537 eb4a4cc41914
permissions -rw-r--r--
check_sources.sh makes me become even less productive
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
     3
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
     4
# subset of subprocess built-in module using posix_spawn rather than fork.
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
     5
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
     6
from __future__ import absolute_import
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
     7
import os
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
     8
import signal
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
     9
import posix_spawn
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    10
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    11
PIPE = "42"
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    12
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    13
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    14
class Popen(object):
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    15
    def __init__(self, args, stdin=None, stdout=None):
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    16
        self.returncode = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    17
        self.stdout = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    18
        self.stdin = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    19
        # TODO: stderr
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    20
        file_actions = posix_spawn.FileActions()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    21
        if stdout is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    22
            # child's stdout, child 2 parent pipe
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    23
            c2pread, c2pwrite = os.pipe()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    24
            # attach child's stdout to writing en of c2p pipe
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    25
            file_actions.add_dup2(c2pwrite, 1)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    26
            # close other end
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    27
            file_actions.add_close(c2pread)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    28
        if stdin is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    29
            # child's stdin, parent to child pipe
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    30
            p2cread, p2cwrite = os.pipe()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    31
            # attach child's stdin to reading en of p2c pipe
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    32
            file_actions.add_dup2(p2cread, 0)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    33
            # close other end
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    34
            file_actions.add_close(p2cwrite)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    35
        self.pid = posix_spawn.posix_spawnp(args[0], args, file_actions=file_actions)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    36
        if stdout is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    37
            self.stdout = os.fdopen(c2pread)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    38
            os.close(c2pwrite)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    39
        if stdin is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    40
            self.stdin = os.fdopen(p2cwrite, 'w')
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    41
            os.close(p2cread)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    42
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    43
    def _wait(self):
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    44
        if self.returncode is None:
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    45
            self.returncode = os.waitpid(self.pid, 0)[1]
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    46
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    47
    def communicate(self):
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    48
        if self.stdin is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    49
            self.stdin.close()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    50
            self.stdin = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    51
        if self.stdout is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    52
            stdoutdata = self.stdout.read()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    53
        else:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    54
            stdoutdata = ""
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    55
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    56
        # TODO
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    57
        stderrdata = ""
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    58
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    59
        self._wait()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    60
        if self.stdout is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    61
            self.stdout.close()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    62
            self.stdout = None
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    63
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    64
        return (stdoutdata, stderrdata)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    65
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    66
    def wait(self):
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    67
        if self.stdin is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    68
            self.stdin.close()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    69
            self.stdin = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    70
        self._wait()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    71
        if self.stdout is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    72
            self.stdout.close()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    73
            self.stdout = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    74
        return self.returncode
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    75
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    76
    def poll(self):
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    77
        if self.returncode is None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    78
            pid, ret = os.waitpid(self.pid, os.WNOHANG)
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    79
            if (pid, ret) != (0, 0):
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    80
                self.returncode = ret
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    81
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    82
                if self.stdin is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    83
                    self.stdin.close()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    84
                    self.stdin = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    85
                if self.stdout is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    86
                    self.stdout.close()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    87
                    self.stdout = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    88
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    89
        return self.returncode
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    90
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    91
    def kill(self):
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    92
        os.kill(self.pid, signal.SIGKILL)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    93
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    94
        if self.stdin is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    95
            self.stdin.close()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    96
            self.stdin = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    97
        if self.stdout is not None:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    98
            self.stdout.close()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    99
            self.stdout = None
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   100
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   101
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   102
def call(*args):
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   103
    cmd = []
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   104
    if isinstance(args[0], str):
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   105
        if len(args) == 1:
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   106
            # oversimplified splitting of arguments,
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   107
            # TODO: care about use of simple and double quotes
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   108
            cmd = args[0].split()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   109
        else:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   110
            cmd = args
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   111
    elif isinstance(args[0], list) and len(args) == 1:
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   112
        cmd = args[0]
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   113
    else:
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   114
        raise Exception("Wrong arguments passed to subprocess.call")
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   115
    pid = posix_spawn.posix_spawnp(cmd[0], cmd)
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   116
    return os.waitpid(pid, 0)
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   117
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   118
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   119
if __name__ == '__main__':
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   120
    # unit test
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   121
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   122
    p = Popen(["tr", "abc", "def"], stdin=PIPE, stdout=PIPE)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   123
    p.stdin.write("blah")
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   124
    p.stdin.close()
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   125
    print(p.stdout.read())
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   126
    p.wait()
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   127
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   128
    p = Popen(["tr", "abc", "def"], stdin=PIPE, stdout=PIPE)
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   129
    p.stdin.write("blah")
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   130
    print(p.communicate())
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   131
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   132
    call("echo blah0")
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   133
    call(["echo", "blah1"])
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   134
    call("echo", "blah2")