Python os.pipe() Examples
The following are 30
code examples of os.pipe().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
os
, or try the search function
.
Example #1
Source File: test_cli.py From ChromaTerm with MIT License | 7 votes |
def test_main_run_pipe_out(): """Have CT run the tty test code with a pipe on stdout.""" _, slave = os.openpty() result = subprocess.run(CLI + ' ' + get_python_command(CODE_ISATTY), check=True, shell=True, stdin=slave, stdout=subprocess.PIPE) assert 'stdin=True, stdout=True' in result.stdout.decode()
Example #2
Source File: vcf_parser.py From gcp-variant-transforms with Apache License 2.0 | 6 votes |
def _init_with_header(self, header_lines): # Following pipe is responsible for supplying lines from child process to # the parent process, which will be fed into PySam object through an actual # file descriptor. return_pipe_read, return_pipe_write = os.pipe() # Since child process doesn't have access to the lines that need to be # parsed, following pipe is needed to supply them from _get_variant() method # into the child process, to be propagated back into the return pipe. send_pipe_read, send_pipe_write = os.pipe() pid = os.fork() if pid: self._process_pid = pid self._init_parent_process(return_pipe_read, send_pipe_write) else: self._init_child_process(send_pipe_read, return_pipe_write, header_lines, self._pre_infer_headers)
Example #3
Source File: iostream_test.py From tornado-zh with MIT License | 6 votes |
def test_pipe_iostream_big_write(self): r, w = os.pipe() rs = PipeIOStream(r, io_loop=self.io_loop) ws = PipeIOStream(w, io_loop=self.io_loop) NUM_BYTES = 1048576 # Write 1MB of data, which should fill the buffer ws.write(b"1" * NUM_BYTES) rs.read_bytes(NUM_BYTES, self.stop) data = self.wait() self.assertEqual(data, b"1" * NUM_BYTES) ws.close() rs.close()
Example #4
Source File: gipc.py From gipc with MIT License | 6 votes |
def _write(self, bindata): """Write `bindata` to pipe in a gevent-cooperative manner. POSIX-compliant system notes (http://linux.die.net/man/7/pipe:): - Since Linux 2.6.11, the pipe capacity is 65536 bytes - Relevant for large messages (O_NONBLOCK enabled, n > PIPE_BUF (4096 Byte, usually)): "If the pipe is full, then write(2) fails, with errno set to EAGAIN. Otherwise, from 1 to n bytes may be written (i.e., a "partial write" may occur; the caller should check the return value from write(2) to see how many bytes were actually written), and these bytes may be interleaved with writes by other processes." EAGAIN is handled within _write_nonblocking; partial writes here. """ bindata = memoryview(bindata) while True: # Causes OSError when read end is closed (broken pipe). bytes_written = _write_nonblocking(self._fd, bindata) if bytes_written == len(bindata): break bindata = bindata[bytes_written:]
Example #5
Source File: iostream.py From tornado-zh with MIT License | 6 votes |
def read_from_fd(self): try: chunk = os.read(self.fd, self.read_chunk_size) except (IOError, OSError) as e: if errno_from_exception(e) in _ERRNO_WOULDBLOCK: return None elif errno_from_exception(e) == errno.EBADF: # If the writing half of a pipe is closed, select will # report it as readable but reads will fail with EBADF. self.close(exc_info=True) return None else: raise if not chunk: self.close() return None return chunk
Example #6
Source File: gipc.py From gipc with MIT License | 6 votes |
def put(self, o): """Encode object ``o`` and write it to the pipe. Block gevent-cooperatively until all data is written. The default encoder is ``pickle.dumps``. :arg o: a Python object that is encodable with the encoder of choice. Raises: - :exc:`GIPCError` - :exc:`GIPCClosed` - :exc:`pickle.PicklingError` """ self._validate() with self._lock: bindata = self._encoder(o) self._write(struct.pack("!i", len(bindata)) + bindata)
Example #7
Source File: popen2.py From meddle with MIT License | 6 votes |
def __init__(self, cmd, bufsize=-1): _cleanup() self.cmd = cmd p2cread, p2cwrite = os.pipe() c2pread, c2pwrite = os.pipe() self.pid = os.fork() if self.pid == 0: # Child os.dup2(p2cread, 0) os.dup2(c2pwrite, 1) os.dup2(c2pwrite, 2) self._run_child(cmd) os.close(p2cread) self.tochild = os.fdopen(p2cwrite, 'w', bufsize) os.close(c2pwrite) self.fromchild = os.fdopen(c2pread, 'r', bufsize)
Example #8
Source File: shell.py From tf-pose with Apache License 2.0 | 6 votes |
def shell(cmd): """Run each line of a shell script; raise an exception if any line returns a nonzero value. """ pin, pout = os.pipe() proc = sp.Popen('/bin/bash', stdin=sp.PIPE) for line in cmd.split('\n'): line = line.strip() if line.startswith('#'): print('\033[33m> ' + line + '\033[0m') else: print('\033[32m> ' + line + '\033[0m') if line.startswith('cd '): os.chdir(line[3:]) proc.stdin.write((line + '\n').encode('utf-8')) proc.stdin.write(('echo $? 1>&%d\n' % pout).encode('utf-8')) ret = "" while not ret.endswith('\n'): ret += os.read(pin, 1) ret = int(ret.strip()) if ret != 0: print("\033[31mLast command returned %d; bailing out.\033[0m" % ret) sys.exit(-1) proc.stdin.close() proc.wait()
Example #9
Source File: iostream_test.py From tornado-zh with MIT License | 6 votes |
def test_pipe_iostream(self): r, w = os.pipe() rs = PipeIOStream(r, io_loop=self.io_loop) ws = PipeIOStream(w, io_loop=self.io_loop) ws.write(b"hel") ws.write(b"lo world") rs.read_until(b' ', callback=self.stop) data = self.wait() self.assertEqual(data, b"hello ") rs.read_bytes(3, self.stop) data = self.wait() self.assertEqual(data, b"wor") ws.close() rs.read_until_close(self.stop) data = self.wait() self.assertEqual(data, b"ld") rs.close()
Example #10
Source File: local_engine.py From gabriel with Apache License 2.0 | 6 votes |
def run(engine_factory, source_name, input_queue_maxsize, port, num_tokens, message_max_size=None): engine_read, server_write = multiprocessing.Pipe(duplex=False) # We cannot read from multiprocessing.Pipe without blocking the event # loop server_read, engine_write = os.pipe() local_server = _LocalServer( num_tokens, input_queue_maxsize, server_write, server_read) local_server.add_source_consumed(source_name) engine_process = multiprocessing.Process( target=_run_engine, args=(engine_factory, engine_read, server_read, engine_write)) try: engine_process.start() os.close(engine_write) local_server.launch(port, message_max_size) finally: local_server.cleanup() os.close(server_read) raise Exception('Server stopped')
Example #11
Source File: iostream.py From tornado-zh with MIT License | 6 votes |
def read_from_fd(self): try: chunk = os.read(self.fd, self.read_chunk_size) except (IOError, OSError) as e: if errno_from_exception(e) in _ERRNO_WOULDBLOCK: return None elif errno_from_exception(e) == errno.EBADF: # If the writing half of a pipe is closed, select will # report it as readable but reads will fail with EBADF. self.close(exc_info=True) return None else: raise if not chunk: self.close() return None return chunk
Example #12
Source File: arbiter.py From jbox with MIT License | 6 votes |
def init_signals(self): """\ Initialize master signal handling. Most of the signals are queued. Child signals only wake up the master. """ # close old PIPE if self.PIPE: [os.close(p) for p in self.PIPE] # initialize the pipe self.PIPE = pair = os.pipe() for p in pair: util.set_non_blocking(p) util.close_on_exec(p) self.log.close_on_exec() # initialize all signals [signal.signal(s, self.signal) for s in self.SIGNALS] signal.signal(signal.SIGCHLD, self.handle_chld)
Example #13
Source File: aaa.py From king-phisher with BSD 3-Clause "New" or "Revised" License | 6 votes |
def send(self, request): """ Encode and send a request through the pipe to the opposite end. This also sets the 'sequence' member of the request and increments the stored value. :param dict request: A request. """ request['sequence'] = self.sequence_number self.sequence_number += 1 if self.sequence_number > 0xffffffff: self.sequence_number = 0 self._raw_send(request) log_msg = "sent request with sequence number {0}".format(request['sequence']) if 'action' in request: log_msg += " and action '{0}'".format(request['action']) self.logger.debug(log_msg)
Example #14
Source File: test_functional.py From pledgeservice with Apache License 2.0 | 6 votes |
def test_it(self): getline = os.path.join(here, 'fixtureapps', 'getline.py') cmds = ( [self.exe, getline, 'http://%s:%d/sleepy' % self.bound_to], [self.exe, getline, 'http://%s:%d/' % self.bound_to] ) r, w = os.pipe() procs = [] for cmd in cmds: procs.append(subprocess.Popen(cmd, stdout=w)) time.sleep(3) for proc in procs: if proc.returncode is not None: # pragma: no cover proc.terminate() # the notsleepy response should always be first returned (it sleeps # for 2 seconds, then returns; the notsleepy response should be # processed in the meantime) result = os.read(r, 10000) os.close(r) os.close(w) self.assertEqual(result, b'notsleepy returnedsleepy returned')
Example #15
Source File: gipc.py From gipc with MIT License | 6 votes |
def _newpipe(encoder, decoder): """Create new pipe via `os.pipe()` and return `(_GIPCReader, _GIPCWriter)` tuple. os.pipe() implementation on Windows (https://goo.gl/CiIWvo): - CreatePipe(&read, &write, NULL, 0) - anonymous pipe, system handles buffer size - anonymous pipes are implemented using named pipes with unique names - asynchronous (overlapped) read and write operations not supported os.pipe() implementation on Unix (http://linux.die.net/man/7/pipe): - based on pipe() - common Linux: pipe buffer is 4096 bytes, pipe capacity is 65536 bytes """ r, w = os.pipe() return (_GIPCReader(r, decoder), _GIPCWriter(w, encoder)) # Define default encoder and decoder functions for pipe data serialization.
Example #16
Source File: rooter.py From ToonRooter with MIT License | 6 votes |
def write_payload(self): port = self._port tar_path = self.create_payload_tar() log.debug(port.read_until("/ # ")) port.write("base64 -d | tar zxf -\n") port.flush() #(tarr, tarw) = os.pipe() #tar = tarfile.open(mode='w|gz', fileobj=tarw) #tar.add("payload/patch_toon.sh") log.info("Transferring payload") with open(tar_path, 'r') as f: base64.encode(f, port) os.remove(tar_path) port.flush() port.reset_input_buffer() port.write("\x04") port.flush()
Example #17
Source File: conftest.py From sentry-python with BSD 2-Clause "Simplified" License | 6 votes |
def capture_events_forksafe(monkeypatch): def inner(): events_r, events_w = os.pipe() events_r = os.fdopen(events_r, "rb", 0) events_w = os.fdopen(events_w, "wb", 0) test_client = sentry_sdk.Hub.current.client old_capture_event = test_client.transport.capture_event def append(event): events_w.write(json.dumps(event).encode("utf-8")) events_w.write(b"\n") return old_capture_event(event) def flush(timeout=None, callback=None): events_w.write(b"flush\n") monkeypatch.setattr(test_client.transport, "capture_event", append) monkeypatch.setattr(test_client, "flush", flush) return EventStreamReader(events_r) return inner
Example #18
Source File: popen2.py From ironpython2 with Apache License 2.0 | 6 votes |
def __init__(self, cmd, bufsize=-1): _cleanup() self.cmd = cmd p2cread, p2cwrite = os.pipe() c2pread, c2pwrite = os.pipe() self.pid = os.fork() if self.pid == 0: # Child os.dup2(p2cread, 0) os.dup2(c2pwrite, 1) os.dup2(c2pwrite, 2) self._run_child(cmd) os.close(p2cread) self.tochild = os.fdopen(p2cwrite, 'w', bufsize) os.close(c2pwrite) self.fromchild = os.fdopen(c2pread, 'r', bufsize)
Example #19
Source File: pipe.py From multibootusb with GNU General Public License v2.0 | 6 votes |
def _pipe2_by_pipe(flags): """A ``pipe2`` implementation using :func:`os.pipe`. ``flags`` is an integer providing the flags to ``pipe2``. .. warning:: This implementation is not atomic! Return a pair of file descriptors ``(r, w)``. """ fds = os.pipe() if flags & os.O_NONBLOCK != 0: for fd in fds: set_fd_status_flag(fd, os.O_NONBLOCK) if flags & O_CLOEXEC != 0: for fd in fds: set_fd_flag(fd, O_CLOEXEC) return fds
Example #20
Source File: popen2.py From ironpython2 with Apache License 2.0 | 5 votes |
def __init__(self, cmd, capturestderr=False, bufsize=-1): """The parameter 'cmd' is the shell command to execute in a sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If 'cmd' is a string it will be passed to the shell (as with os.system()). The 'capturestderr' flag, if true, specifies that the object should capture standard error output of the child process. The default is false. If the 'bufsize' parameter is specified, it specifies the size of the I/O buffers to/from the child process.""" _cleanup() self.cmd = cmd p2cread, p2cwrite = os.pipe() c2pread, c2pwrite = os.pipe() if capturestderr: errout, errin = os.pipe() self.pid = os.fork() if self.pid == 0: # Child os.dup2(p2cread, 0) os.dup2(c2pwrite, 1) if capturestderr: os.dup2(errin, 2) self._run_child(cmd) os.close(p2cread) self.tochild = os.fdopen(p2cwrite, 'w', bufsize) os.close(c2pwrite) self.fromchild = os.fdopen(c2pread, 'r', bufsize) if capturestderr: os.close(errin) self.childerr = os.fdopen(errout, 'r', bufsize) else: self.childerr = None
Example #21
Source File: Agent.py From pcocc with GNU General Public License v3.0 | 5 votes |
def add_intr_handler(cls, cluster, indices, execid, ctx): cls._lock.acquire() cls._registered_execs.append((cluster, indices, execid, ctx)) if len(cls._registered_execs) == 1: cls._intr_r = fake_signalfd([signal.SIGINT, signal.SIGTERM]) cls._stop_sig_r, cls._stop_sig_w = os.pipe() cls._intr_th_instance = threading.Thread(None, cls.intr_handler_th, None, args=(cls._intr_r, cls._stop_sig_r)) cls._intr_th_instance.start() cls._lock.release()
Example #22
Source File: trigger.py From pledgeservice with Apache License 2.0 | 5 votes |
def __init__(self, map): _triggerbase.__init__(self) r, self.trigger = self._fds = os.pipe() asyncore.file_dispatcher.__init__(self, r, map=map)
Example #23
Source File: test_functional.py From pledgeservice with Apache License 2.0 | 5 votes |
def send_check_error(self, to_send): # Unlike inet domain sockets, Unix domain sockets can trigger a # 'Broken pipe' error when the socket it closed. try: self.sock.send(to_send) except socket.error as exc: self.assertEqual(get_errno(exc), errno.EPIPE)
Example #24
Source File: popen2.py From meddle with MIT License | 5 votes |
def __init__(self, cmd, capturestderr=False, bufsize=-1): """The parameter 'cmd' is the shell command to execute in a sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If 'cmd' is a string it will be passed to the shell (as with os.system()). The 'capturestderr' flag, if true, specifies that the object should capture standard error output of the child process. The default is false. If the 'bufsize' parameter is specified, it specifies the size of the I/O buffers to/from the child process.""" _cleanup() self.cmd = cmd p2cread, p2cwrite = os.pipe() c2pread, c2pwrite = os.pipe() if capturestderr: errout, errin = os.pipe() self.pid = os.fork() if self.pid == 0: # Child os.dup2(p2cread, 0) os.dup2(c2pwrite, 1) if capturestderr: os.dup2(errin, 2) self._run_child(cmd) os.close(p2cread) self.tochild = os.fdopen(p2cwrite, 'w', bufsize) os.close(c2pwrite) self.fromchild = os.fdopen(c2pread, 'r', bufsize) if capturestderr: os.close(errin) self.childerr = os.fdopen(errout, 'r', bufsize) else: self.childerr = None
Example #25
Source File: test_visualize.py From automat with MIT License | 5 votes |
def isGraphvizInstalled(): """ Are the graphviz tools installed? """ r, w = os.pipe() os.close(w) try: return not subprocess.call("dot", stdin=r, shell=True) finally: os.close(r)
Example #26
Source File: test_cli.py From ChromaTerm with MIT License | 5 votes |
def test_main_stdin_processing(): """General stdin processing with relation to READ_SIZE and WAIT_FOR_SPLIT.""" try: stdin_r, stdin_w = os.pipe() stdout_r, stdout_w = os.pipe() process = subprocess.Popen(CLI, shell=True, stdin=stdin_r, stdout=stdout_w) time.sleep(0.5) # Any startup delay assert process.poll() is None os.write(stdin_w, b'Hello world\n') time.sleep(0.1) # Any processing delay assert os.read(stdout_r, 100) == b'Hello world\n' os.write(stdin_w, b'Hey there') time.sleep(0.1 + chromaterm.cli.WAIT_FOR_SPLIT) # Include split wait assert os.read(stdout_r, 100) == b'Hey there' write_size = chromaterm.cli.READ_SIZE + 1 os.write(stdin_w, b'x' * write_size) time.sleep(0.1 + chromaterm.cli.WAIT_FOR_SPLIT) # Include split wait assert os.read(stdout_r, write_size * 2) == b'x' * write_size finally: os.close(stdin_r) os.close(stdin_w) os.close(stdout_r) os.close(stdout_w) process.kill()
Example #27
Source File: test_cli.py From ChromaTerm with MIT License | 5 votes |
def test_main_broken_pipe(): """Break a pipe while CT is trying to write to it. The echo at the end will close before CT has had the chance to write to the pipe.""" result = subprocess.run('echo | ' + CLI + ' | echo', check=False, shell=True, stderr=subprocess.PIPE) assert b'Broken pipe' not in result.stderr
Example #28
Source File: test_cli.py From ChromaTerm with MIT License | 5 votes |
def test_read_ready_timeout_input(): """Immediate ready with timeout when there is input buffered.""" pipe_r, pipe_w = os.pipe() os.write(pipe_w, b'Hello world') before = time.time() assert chromaterm.cli.read_ready(pipe_r, timeout=0.1) after = time.time() assert after - before < 0.1
Example #29
Source File: test_cli.py From ChromaTerm with MIT License | 5 votes |
def test_read_ready_timeout_empty(): """Wait with no input.""" pipe_r, _ = os.pipe() before = time.time() assert not chromaterm.cli.read_ready(pipe_r, timeout=0.1) after = time.time() assert after - before >= 0.1
Example #30
Source File: test_cli.py From ChromaTerm with MIT License | 5 votes |
def test_read_ready_input(): """Immediate ready when there is input buffered.""" pipe_r, pipe_w = os.pipe() os.write(pipe_w, b'Hello world') assert chromaterm.cli.read_ready(pipe_r)