runtime/spawn_subprocess.py
author Edouard Tisserant
Tue, 23 Oct 2018 16:13:34 +0200
changeset 2322 7ce4e5cf6339
child 2492 7dd551ac2fa0
permissions -rw-r--r--
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.
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
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
     6
import posix_spawn
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
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
     9
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
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
    11
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
class Popen(object):
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
    13
    def __init__(self, args,stdin=None, 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
    14
        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
    15
        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
    16
        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
    17
        # 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
    18
        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
    19
        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
    20
            # 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
    21
            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
    22
            # 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
    23
            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
    24
            # 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
    25
            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
    26
        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
    27
            # 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
    28
            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
    29
            # 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
    30
            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
    31
            # 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
    32
            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
    33
        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
    34
        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
    35
            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
    36
            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
    37
        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
    38
            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
    39
            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
    40
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
    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
    42
        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
    43
            self.returncode = os.waitpid(self.pid,0)[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
    44
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
    45
    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
    46
        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
    47
            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
    48
            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
    49
        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
    50
            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
    51
        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
    52
            stdoutdata = ""
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
        
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
        # 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
    55
        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
    56
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
        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
    58
        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
    59
            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
    60
            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
    61
        
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
        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
    63
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
    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
    65
        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
    66
            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
    67
            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
    68
        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
    69
        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
    70
            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
    71
            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
    72
        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
    73
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
    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
    75
        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
    76
            pid, ret = os.waitpid(self.pid, os.WNOHANG)
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 (pid,ret) != (0,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
    78
                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
    79
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
                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
    81
                    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
    82
                    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
    83
                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
    84
                    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
    85
                    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
    86
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
        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
    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
    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
    90
        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
    91
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
        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
    93
            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
    94
            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
    95
        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
    96
            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
    97
            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
    98
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
        
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
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
   102
    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
   103
    if isinstance(args[0], str):
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 len(args)==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
   105
            # 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
   106
            # 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
   107
            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
   108
        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
   109
            cmd = 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
   110
    elif isinstance(args[0], list) and len(args)==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
   111
        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
   112
    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
   113
        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
   114
    pid = posix_spawn.posix_spawnp(cmd[0], 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
   115
    return os.waitpid(pid,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
   116
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
   117
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
   118
    # 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
   119
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
    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
   121
    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
   122
    p.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
   123
    print p.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
   124
    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
   125
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 = 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
   127
    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
   128
    print p.communicate()
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
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
   130
    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
   131
    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
   132
    call("echo", "blah2")