di emulare il bash process substitution:
tubi
#!/usr/bin/env python
from subprocess import check_call
check_call('someprogram <(someprocess) <(anotherprocess)',
shell=True, executable='/bin/bash')
In Python, è possibile utilizzare nome:
#!/usr/bin/env python
from subprocess import Popen
with named_pipes(n=2) as paths:
someprogram = Popen(['someprogram'] + paths)
processes = []
for path, command in zip(paths, ['someprocess', 'anotherprocess']):
with open(path, 'wb', 0) as pipe:
processes.append(Popen(command, stdout=pipe, close_fds=True))
for p in [someprogram] + processes:
p.wait()
dove named_pipes(n)
è:
import os
import shutil
import tempfile
from contextlib import contextmanager
@contextmanager
def named_pipes(n=1):
dirname = tempfile.mkdtemp()
try:
paths = [os.path.join(dirname, 'named_pipe' + str(i)) for i in range(n)]
for path in paths:
os.mkfifo(path)
yield paths
finally:
shutil.rmtree(dirname)
Un altro e più pre modo agevole (non è necessario creare una voce con nome sul disco) per implementare la sostituzione del processo di bash è utilizzare i nomi di file /dev/fd/N
(se disponibili) come suggested by @Dunes. Su FreeBSD, fdescfs(5)
(/dev/fd/#
) creates entries for all file descriptors opened by the process. Per verificare la disponibilità, eseguire:
$ test -r /dev/fd/3 3</dev/null && echo /dev/fd is available
Se fallisce; cercare di link simbolico /dev/fd
a proc(5)
come si fa su alcuni sistemi Linux:
$ ln -s /proc/self/fd /dev/fd
Ecco /dev/fd
based realizzazione di comando someprogram <(someprocess) <(anotherprocess)
bash:
#!/usr/bin/env python3
from contextlib import ExitStack
from subprocess import CalledProcessError, Popen, PIPE
def kill(process):
if process.poll() is None: # still running
process.kill()
with ExitStack() as stack: # for proper cleanup
processes = []
for command in [['someprocess'], ['anotherprocess']]: # start child processes
processes.append(stack.enter_context(Popen(command, stdout=PIPE)))
stack.callback(kill, processes[-1]) # kill on someprogram exit
fds = [p.stdout.fileno() for p in processes]
someprogram = stack.enter_context(
Popen(['someprogram'] + ['/dev/fd/%d' % fd for fd in fds], pass_fds=fds))
for p in processes: # close pipes in the parent
p.stdout.close()
# exit stack: wait for processes
if someprogram.returncode != 0: # errors shouldn't go unnoticed
raise CalledProcessError(someprogram.returncode, someprogram.args)
Nota: sulla mia macchina Ubuntu, il codice subprocess
funziona solo in Python 3.4+, nonostante pass_fds
sia disponibile da Python 3.2.
correlato: [Bash processo di sostituzione con stile Popen di Python] (http://stackoverflow.com/q/15343447/4279) – jfs
relativi al titolo: [Come si usa subprocess.Popen per collegare più processi da tubi?] (http://stackoverflow.com/q/295459/4279) – jfs