Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7163337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:53:10+00:00 2026-05-28T13:53:10+00:00

I am trying to permit a python app to access various locations in a

  • 0

I am trying to permit a python app to access various locations in a many-GB file stored in S3. I’d like to create a drop-in replacement file-like object that intelligently downloads chunks of data from S3 in a separate thread to meet seek() and read() requests.

Is there a simple data structure I can use to store arbitrary intervals of the file?

It must support O(log n) look-up and O(n) insertion (n=number of chunks, not size of file). It will also need to support quickly querying for gaps so that the load thread can efficiently find the next chunk it should download. This is currently not supported by things like SortedCollection, suggesting I may need to manually use bisect_* in a new container.

Example usage is:

import os
import time
from bigfile import BigFile

chunksize = (2**20)*64 # 64MB

bf = BigFile('my_bucket', 'key_name', chunksize=chunksize)

# read from beginning (blocks until first chunk arrives)
bf.read(100)

# continues downloading subsequent chunks in background
time.sleep(10)

# seek into second chunk and read (should not block)
bf.seek(blocksize, os.SEEK_SET)
bf.read(100)

# seek far into the file
bf.seek(blocksize*100 + 54, os.SEEK_SET) # triggers chunk download starting at new location
bf.read(100) # blocks until chunk arrives

# seek back to beginning (should not block, already have this chunk)
bf.seek(0, os.SEEK_SET)
bf.read(100)

# read entire rest of file (blocks until all chunks are downloaded)
bf.read()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:53:11+00:00Added an answer on May 28, 2026 at 1:53 pm

    This implementation uses chunks of fixed size and offsets. If the chunks are very large and the network is very slow, reads may block for a long time (consider a read starting at the last byte of a chunk, it would have to wait for the entire previous chunk to load, then the next chunk).

    Ideally we could use chunks of arbitrary size and location, so we can optimize loads to start at exactly the read point. But below is a good 80% solution.

    import boto
    import threading
    import tempfile
    import os
    
    DEFAULT_CHUNK_SIZE = 2**20 * 64 # 64 MB per request
    
    class BigFile(object):
        def __init__(self, file_obj, file_size, chunksize=DEFAULT_CHUNK_SIZE, start=True):
            self._file_obj = file_obj
            self._file_size = file_size
            self._lock = threading.RLock()
            self._load_condition = threading.Condition(self._lock)
            self._load_run = True
            self._loc = 0
            self._chunk_size = chunksize
            chunk_count = self._file_size // self._chunk_size
            chunk_count += 1 if self._file_size % self._chunk_size else 0
            self._chunks = [None for _ in xrange(chunk_count)]
            self._load_thread = threading.Thread(target=self._load)
            if start:
                self._load_thread.start()
    
        def _chunk_loc(self):
            ' Returns (chunk_num, chunk_offset) for a given location in the larger file '
            return self._loc // self._chunk_size, self._loc % self._chunk_size
    
        def _load_chunk(self, chunk_num):
            tf = tempfile.TemporaryFile()
            start_idx = chunk_num * self._chunk_size
            self._file_obj.seek(start_idx)
            tf.write(self._file_obj.read(self._chunk_size))
            with self._lock:
                self._chunks[chunk_num] = (tf, tf.tell()) # (tempfile, size)
                self._load_condition.notify()
    
        def _load(self):
            while self._load_run:
                # check current chunk, load if needed
                with self._lock:
                    chunk_num, _ = self._chunk_loc()
                    chunk_and_size = self._chunks[chunk_num]
                if chunk_and_size is None:
                    self._load_chunk(chunk_num)
    
                # find next empty chunk
                for i in xrange(len(self._chunks)):
                    cur_chunk = chunk_num + i
                        cur_chunk %= len(self._chunks) # loop around
                    if self._chunks[cur_chunk] is None:
                        self._load_chunk(cur_chunk)
                        break
                else:
                    # all done, stop thread
                    break
    
        def seek(self, loc, rel=os.SEEK_SET):
            with self._lock:
                if rel == os.SEEK_CUR:
                    self._loc += loc
                elif rel == os.SEEK_SET:
                    self._loc = loc
                elif rel == os.SEEK_END:
                    self._loc = self._file_size + loc
    
        def read(self, bytes_to_read):
            ret = []
            with self._lock:
                chunk_num, chunk_offset = self._chunk_loc()
                while (bytes_to_read > 0 or bytes_to_read == -1) and chunk_num < len(self._chunks):
                    while not self._chunks[chunk_num]:
                        self._load_condition.wait()
                    chunk, size = self._chunks[chunk_num]
                    cur_chunk_bytes = min(self._chunk_size-chunk_offset, bytes_to_read, size)
                    chunk.seek(chunk_offset, os.SEEK_SET)
                    data = chunk.read(cur_chunk_bytes)
                    ret.append(data)
                    bytes_to_read -= len(data)
                    chunk_num += 1
            return ''.join(ret)
    
        def start(self):
            self._load_thread.start()
    
        def join(self):
            self._load_thread.join()
    
        def stop(self):
            self._load_run = False
    
    class S3RangeReader:
        def __init__(self, key_obj):
            self._key_obj = key_obj
            self.size = self._key_obj.size
            self._pos = 0
    
        def __len__(self):
            return self.size
    
        def seek(self, pos, rel=os.SEEK_SET):
            if rel == os.SEEK_CUR:
                self._pos += pos
            elif rel == os.SEEK_SET:
                self._pos = pos
            elif rel == os.SEEK_END:
                self._pos = self.size + pos
    
        def read(self, bytes=-1):
            if bytes == 0 or self._pos >= self.size:
                return ''
            else:
                if bytes == -1:
                    bytes = self.size
                headers = {'Range': 'bytes=%s-%s' % (self._pos, self._pos + bytes - 1)} # S3 ranges are closed ranges: [start,end]
                return self._key_obj.get_contents_as_string(headers=headers)
    
    if __name__ == '__main__':
        key = boto.s3_connect().get_bucket('mybucket').get_key('my_key')
        reader = S3RangeReader(key)
        bf = BigFile(reader, len(reader)) # download starts by default
        bf.seek(1000000)
        bf.read(100) # blocks
        bf.seek(0)
        bf.read(100) # should not block
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get my css / C# functions to look like this: body {
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
Trying to honor a feature request from our customers, I'd like that my application,
I'm trying to set up the auth dialog of my facebook app to only
I'm trying to permit debug logging per a particular class using Log4j, and I've
So, we have an old VB 6 App written many many years ago. It
I'm trying to get the permit method to work using the rails-authorization-plugin and authlogic
I'm trying to overcome the following situation. Given a directory stored on an NTFS
I'm trying to connect from a web app to another web app using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.