runtime/spawn_subprocess.py
author Edouard Tisserant
Fri, 24 Nov 2023 12:16:19 +0100
changeset 3860 a1d7187b8402
parent 3293 d2b0c768755d
child 3881 0b3ac94f494c
permissions -rw-r--r--
Runtime: Add stderr support to local posix spawn based subprocess replacement
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):
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    17
    def __init__(self, args, stdin=None, stdout=None, stderr=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
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    20
        self.stderr = 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
    21
        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
    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
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    25
            c1pread, c1pwrite = os.pipe()
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    26
            # attach child's stdout to writing en of c1p pipe
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    27
            file_actions.add_dup2(c1pwrite, 1)
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    28
            # close other end
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    29
            file_actions.add_close(c1pread)
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    30
        if stderr is not None:
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    31
            # child's stderr, child 2 parent pipe
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
    32
            c2pread, c2pwrite = os.pipe()
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    33
            # attach child's stderr to writing en of c2p pipe
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    34
            file_actions.add_dup2(c2pwrite, 2)
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
    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(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
    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
            # 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
    39
            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
    40
            # 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
    41
            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
    42
            # 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
    43
            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
    44
        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
    45
        if stdout is not None:
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    46
            self.stdout = os.fdopen(c1pread)
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    47
            os.close(c1pwrite)
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    48
        if stderr is not None:
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    49
            self.stderr = os.fdopen(c2pread)
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
    50
            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
    51
        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
    52
            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
    53
            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
    54
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
    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
    56
        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
    57
            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
    58
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    59
    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
    60
        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
    61
            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
    62
            self.stdin = None
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    63
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    64
        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
    65
            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
    66
        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
    67
            stdoutdata = ""
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    68
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    69
        if self.stderr is not None:
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    70
            stderrdata = self.stderr.read()
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    71
        else:
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    72
            stderrdata = ""
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
    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
        self._wait()
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    75
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
    76
        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
    77
            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
    78
            self.stdout = None
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
    79
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    80
        if self.stderr is not None:
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    81
            self.stderr.close()
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    82
            self.stderr = None
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    83
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
    84
        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
    85
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
    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
    87
        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
    88
            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
    89
            self.stdin = None
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    90
2322
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
    91
        self._wait()
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
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
        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
    94
            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
    95
            self.stdout = None
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    96
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    97
        if self.stderr is not None:
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    98
            self.stderr.close()
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
    99
            self.stderr = None
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   100
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
   101
        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
   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 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
   104
        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
   105
            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
   106
            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
   107
                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
   108
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork 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
                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
   110
                    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
   111
                    self.stdin = None
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   112
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
                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
   114
                    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
   115
                    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
   116
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   117
                if self.stderr is not None:
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   118
                    self.stderr.close()
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   119
                    self.stderr = None
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   120
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
   121
        return self.returncode
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   122
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
   123
    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
   124
        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
   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
        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
   127
            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
   128
            self.stdin = None
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   129
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
   130
        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
   131
            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
   132
            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
   133
3860
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   134
        if self.stderr is not None:
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   135
            self.stderr.close()
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   136
            self.stderr = None
a1d7187b8402 Runtime: Add stderr support to local posix spawn based subprocess replacement
Edouard Tisserant
parents: 3293
diff changeset
   137
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
   138
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   139
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
   140
    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
   141
    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
   142
        if len(args) == 1:
3293
d2b0c768755d Runtime: Better handling of nested strings in spawn_subprocess commandline parsing
Edouard Tisserant
parents: 2537
diff changeset
   143
            # splitting of arguments that cares about 
d2b0c768755d Runtime: Better handling of nested strings in spawn_subprocess commandline parsing
Edouard Tisserant
parents: 2537
diff changeset
   144
            # use of simple and double quotes
d2b0c768755d Runtime: Better handling of nested strings in spawn_subprocess commandline parsing
Edouard Tisserant
parents: 2537
diff changeset
   145
            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
   146
        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
   147
            cmd = args
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   148
    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
   149
        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
   150
    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
   151
        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
   152
    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
   153
    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
   154
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
   155
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   156
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
   157
    # 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
   158
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   159
    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
   160
    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
   161
    p.stdin.close()
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2322
diff changeset
   162
    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
   163
    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
   164
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   165
    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
   166
    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
   167
    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
   168
7ce4e5cf6339 Added runtime/spawn_subprocess.py. Force use posix spawn instead of fork, with API similar to subprocess. Using fork in runtime is incompatible with Xenomai, because memory is locked and this can lead to out of memory error.
Edouard Tisserant
parents:
diff changeset
   169
    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
   170
    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
   171
    call("echo", "blah2")