-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe_buffer.py
40 lines (29 loc) · 988 Bytes
/
pipe_buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
import fcntl
import os
from splicy.splice import F_SETPIPE_SZ, F_GETPIPE_SZ
class PipeBuffer(object):
PIPE_BUFFER_SIZE = 2**20
def __init__(self, size = PIPE_BUFFER_SIZE):
# FIXME: in case of EMFILE when calling os.pipe(), self.reader
# and self.writer can be uninitialised, which will raise an
# AttributeError in __del__()
self.reader, self.writer = os.pipe()
self.size = size
@property
def size(self):
return self._size
@size.setter
def size(self, value):
# FIXME: self._size is not the *real* size of the buffer
self._size = value
return fcntl.fcntl(self.reader, F_SETPIPE_SZ, value)
def write(self, data):
return os.write(self.writer, data)
def read(self, buffersize):
return os.read(self.reader, buffersize)
def close(self):
try:
os.close(self.reader)
finally:
os.close(self.writer)