runtime/spawn_subprocess.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 02 Mar 2021 09:28:44 +0100
changeset 2729 4e0cd7806776
parent 2537 eb4a4cc41914
child 3293 d2b0c768755d
permissions -rw-r--r--
IDE: Iec2CSettings: Log wasn't available in case of exception when launching iec2c.
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
2537
eb4a4cc41914 Fix various pylint and pep8 errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2492
diff changeset
     6
from __future__ import print_function
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
     7
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
     8
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
     9
import signal
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    10
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
    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
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
    13
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    14
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
    15
class Popen(object):
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    16
    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
    17
        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
    18
        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
    19
        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
    20
        # 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
    21
        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
    22
        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
    23
            # 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
    24
            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
    25
            # 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
    26
            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
    27
            # 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
    28
            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
    29
        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
    30
            # 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
    31
            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
    32
            # 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
    33
            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
    34
            # 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
    35
            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
    36
        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
    37
        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
    38
            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
    39
            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
    40
        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
    41
            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
    42
            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
    43
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
    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
    45
        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
    46
            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
    47
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
    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
    49
        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
    50
            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
    51
            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
    52
        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
    53
            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
    54
        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
    55
            stdoutdata = ""
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    56
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
    57
        # 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
    58
        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
    59
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._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
    61
        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
    62
            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
    63
            self.stdout = None
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    64
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
    65
        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
    66
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
    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
    68
        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
    69
            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
    70
            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
    71
        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
    72
        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
    73
            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
    74
            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
    75
        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
    76
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
    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
    78
        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
    79
            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
    80
            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
    81
                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
    82
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.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
    84
                    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
    85
                    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
    86
                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
    87
                    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
    88
                    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
    89
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
        return self.returncode
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    91
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
    92
    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
    93
        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
    94
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.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
    96
            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
    97
            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
    98
        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
    99
            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
   100
            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
   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
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
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
   104
    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
   105
    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
   106
        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
   107
            # 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
   108
            # 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
   109
            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
   110
        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
   111
            cmd = args
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   112
    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
   113
        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
   114
    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
   115
        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
   116
    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
   117
    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
   118
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
   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
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
   121
    # 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
   122
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 = 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
   124
    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
   125
    p.stdin.close()
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   126
    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
   127
    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
   128
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 = 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
   130
    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
   131
    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
   132
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 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
   134
    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
   135
    call("echo", "blah2")