structio/structio/parallel.py

40 lines
1.1 KiB
Python
Raw Normal View History

# Module inspired by subprocess which allows for asynchronous
# multiprocessing
import os
import subprocess
from subprocess import (
CalledProcessError,
CompletedProcess,
SubprocessError,
STDOUT,
DEVNULL,
PIPE
)
class Popen:
"""
Wrapper around subprocess.Popen, but async
"""
def __init__(self, *args, **kwargs):
"""
Public object constructor
"""
if "universal_newlines" in kwargs:
# Not sure why? But everyone else is doing it so :shrug:
raise RuntimeError("universal_newlines is not supported")
if stdin := kwargs.get("stdin"):
# Curio mentions stuff breaking if the child process
# is passed a stdin fd that is set to non-blocking mode
if hasattr(os, "set_blocking"):
os.set_blocking(stdin.fileno(), True)
# Delegate to Popen's constructor
self._process = subprocess.Popen(*args, **kwargs)
def __getattr__(self, item):
# Delegate to internal process object
return getattr(self._process, item)