runtime/spawn_subprocess.py
author Edouard Tisserant
Fri, 21 Oct 2022 10:39:43 +0200
changeset 3648 ff42600fddd7
parent 3293 d2b0c768755d
child 3750 f62625418bff
child 3860 a1d7187b8402
permissions -rw-r--r--
SVGHMI: add automatic reconnection of websocket with 1s reconnection delay after first attempt.
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
3293
d2b0c768755d Runtime: Better handling of nested strings in spawn_subprocess commandline parsing
Edouard Tisserant
parents: 2537
diff changeset
    10
import shlex
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    11
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
    12
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
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
    14
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    15
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
class Popen(object):
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    17
    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
    18
        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
    19
        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
    20
        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
    21
        # 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
    22
        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
    23
        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
    24
            # 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
    25
            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
    26
            # 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
    27
            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
    28
            # 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
    29
            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
    30
        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
    31
            # 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
    32
            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
    33
            # 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
    34
            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
    35
            # 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
    36
            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
    37
        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
    38
        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
    39
            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
    40
            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
    41
        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
    42
            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
    43
            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
    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 _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
    46
        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
    47
            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
    48
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
    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
    50
        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
    51
            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
    52
            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
    53
        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
    54
            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
    55
        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
    56
            stdoutdata = ""
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    57
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
    58
        # 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
    59
        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
    60
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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._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
    62
        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
    63
            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
    64
            self.stdout = None
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    65
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
    66
        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
    67
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
    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
    69
        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
    70
            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
    71
            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
    72
        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
    73
        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
    74
            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
    75
            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
    76
        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
    77
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
    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
    79
        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
    80
            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
    81
            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
    82
                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
    83
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
                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
    85
                    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
    86
                    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
    87
                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
    88
                    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
    89
                    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
    90
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
        return self.returncode
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    92
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
    93
    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
    94
        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
    95
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
        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
    97
            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
    98
            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
    99
        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
   100
            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
   101
            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
   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
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
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
   105
    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
   106
    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
   107
        if len(args) == 1:
3293
d2b0c768755d Runtime: Better handling of nested strings in spawn_subprocess commandline parsing
Edouard Tisserant
parents: 2537
diff changeset
   108
            # splitting of arguments that cares about 
d2b0c768755d Runtime: Better handling of nested strings in spawn_subprocess commandline parsing
Edouard Tisserant
parents: 2537
diff changeset
   109
            # use of simple and double quotes
d2b0c768755d Runtime: Better handling of nested strings in spawn_subprocess commandline parsing
Edouard Tisserant
parents: 2537
diff changeset
   110
            cmd = shlex.split(args[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
   111
        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
   112
            cmd = args
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   113
    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
   114
        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
   115
    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
   116
        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
   117
    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
   118
    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
   119
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
   120
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
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
   122
    # 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
   123
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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 = 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
   125
    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
   126
    p.stdin.close()
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   127
    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
   128
    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
   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
    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
   131
    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
   132
    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
   133
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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 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
   135
    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
   136
    call("echo", "blah2")