| 757 | self.buffer = StringIO() |
| 758 | |
| 759 | def write(self, content): |
| 760 | # without a "with" statement, the content is written immediately without buffer |
| 761 | # when writing a large batch of contents at a time, this will be quite slow |
| 762 | import oss2 |
| 763 | buffer = self.buffer.getvalue() |
| 764 | if buffer: |
| 765 | content = buffer + content |
| 766 | self.buffer.close() |
| 767 | self.buffer = StringIO() |
| 768 | try: |
| 769 | result = self.bucket.append_object(self.path, self.position, |
| 770 | content) |
| 771 | self.position = result.next_position |
| 772 | except oss2.exceptions.PositionNotEqualToLength: |
| 773 | raise RuntimeError( |
| 774 | f'Race condition detected. It usually means multiple programs were writing to the same file' |
| 775 | f'{OSS_PREFIX}{self.bucket.bucket_name}/{self.path} (Error 409: PositionNotEqualToLength)' |
| 776 | ) |
| 777 | except (oss2.exceptions.RequestError, |
| 778 | oss2.exceptions.ServerError) as e: |
| 779 | self.buffer.write(content) |
| 780 | logging.error( |
| 781 | str(e) + |
| 782 | f'when writing to {OSS_PREFIX}{self.bucket.bucket_name}/{self.path}. Content buffered.' |
| 783 | ) |
| 784 | |
| 785 | def flush(self, retry=0): |
| 786 | import oss2 |