nonblock (version 4.0.0)
index

Copyright (c) 2015-2016 Timothy Savannah under terms of LGPLv2. You should have received a copy of this LICENSE with this distribution.
 
Contains pure-python functions for non-blocking IO in python

 
Package Contents
       
BackgroundRead
BackgroundWrite
common
read

 
Classes
       
builtins.object
nonblock.BackgroundWrite.BackgroundIOPriority

 
class BackgroundIOPriority(builtins.object)
    BackgroundIOPriority - Priority Profile for doing background writes.
 
    See __init__ for fields
 
  Methods defined here:
__getitem__(self, key)
__init__(self, chainPollTime, defaultChunkSize, bandwidthPct, numChunksRateSmoothing=5)
__init__ - Create a BackgroundIOPriority
 
Some terms: throughput - Bandwidth out (Megs per second)
            interactivity - CPU time available for other tasks (calculations, other I/O, etc)
 
@param chainPollTime - float > 0, When chaining, this is the sleep time between checking if prior is finished.
    Too low and the polling takes up CPU time, too high and you'll lose a little time in between chained writes, while gaining interactivity elsewhere.
 
@param defaultChunkSize - integer > 0, When providing a straight string/bytes to bgwrite (instead of chunking yourself, or using bgwrite_chunk) this will
    be used as the max size of each chunk. Each chunk is written and a flush is issued (if the stream supports it).
    Increasing this increases throughput while decreasing interactivity
 
@param bandwidthPct - integer > 0 and < 100. This is the percentage of overall bandwidth that this task will attempt to use.
 
  A high number means higher throughput at the cost of lest interactivity for other tasks, a low number means the opposite.
 
  So, for example, a bandwidthPct of "50" will attempt to use "50%" of the available bandwidth. Note, this does not represent theroetical
  max bandwidth, i.e. the max rate of the I/O device, but the amount of available bandwidth available to this application. For example,
  if this is given "100%", no throttling is performed. If this is given "80%", then it calculates the average time to write a single chunk,
  ( see #numChunksRateSmoothing for how many chunks are used in evaluating this average ), and sleeps for then 20% of that time at the end
  of every chunk. 
 
@param numChunksRateSmoothing - integer >= 1 , Default 5. This is the number of chunks which are used in calculating the current throughput rate.
  See #bandwidthPct for the other half of the story. The higher this number, the more "fair" your application will be against a constant
  rate of I/O by other applications, but the less able it may be to play fair when the external I/O is spiking.
 
  Also, consider that this is related to the #defaultChunkSize, as it is not a constant period of time. The default of "5" should be okay,
  but you may want to tune it if you use really large or really small chunk sizes.
 
 
An "interactivity score" is defined to be (number of calculations) / (time to write data).
__setitem__(self, key, value)

Data descriptors defined here:
bandwidthPct
chainPollTime
defaultChunkSize
numChunksRateSmoothing

 
Functions
       
bgread(stream, blockSizeLimit=65535, pollTime=0.03, closeStream=True)
bgread - Start a thread which will read from the given stream in a non-blocking fashion, and automatically populate data in the returned object.
 
    @param stream <object> - A stream on which to read. Socket, file, etc.
 
    @param blockSizeLimit <None/int> - Number of bytes. Default 65535.
 
        If None, the stream will be read from until there is no more available data (not closed, but you've read all that's been flushed to straem). This is okay for smaller datasets, but this number effectively controls the amount of CPU time spent in I/O on this stream VS everything else in your application. The default of 65535 bytes is a fair amount of data.
 
    @param pollTime <float> - Default .03 (30ms) After all available data has been read from the stream, wait this many seconds before checking again for more data.
        
        A low number here means a high priority, i.e. more cycles will be devoted to checking and collecting the background data. Since this is a non-blocking read, this value is the "block", which will return execution context to the remainder of the application. The default of 100ms should be fine in most cases. If it's really idle data collection, you may want to try a value of 1 second.
 
    @param closeStream <bool> - Default True. If True, the "close" method on the stream object will be called when the other side has closed and all data has been read.
 
 
 
NOTES --
 
        blockSizeLimit / pollTime is your effective max-throughput. Real throughput will be lower than this number, as the actual throughput is be defined by:
 
        T = (blockSizeLimit / pollTime) - DeviceReadTime(blockSizeLimit)
 
    Using the defaults of .03 and 65535 means you'll read up to 2 MB per second. Keep in mind that the more time spent in I/O means less time spent doing other tasks.
 
 
    @return - The return of this function is a BackgroundReadData object. This object contains an attribute "blocks" which is a list of the non-zero-length blocks that were read from the stream. The object also contains a calculated property, "data", which is a string/bytes (depending on stream mode) of all the data currently read. The property "isFinished" will be set to True when the stream has been closed. The property "error" will be set to any exception that occurs during reading which will terminate the thread. @see BackgroundReadData for more info.
bgwrite(fileObj, data, closeWhenFinished=False, chainAfter=None, ioPrio=4)
bgwrite - Start a background writing process
 
    @param fileObj <stream> - A stream backed by an fd
 
    @param data    <str/bytes/list> - The data to write. If a list is given, each successive element will be written to the fileObj and flushed. If a string/bytes is provided, it will be chunked according to the #BackgroundIOPriority chosen. If you would like a different chunking than the chosen ioPrio provides, use #bgwrite_chunk function instead.
 
       Chunking makes the data available quicker on the other side, reduces iowait on this side, and thus increases interactivity (at penalty of throughput).
 
    @param closeWhenFinished <bool> - If True, the given fileObj will be closed after all the data has been written. Default False.
 
    @param chainAfter  <None/BackgroundWriteProcess> - If a BackgroundWriteProcess object is provided (the return of bgwrite* functions), this data will be held for writing until the data associated with the provided object has completed writing.
    Use this to queue several background writes, but retain order within the resulting stream.
 
 
    @return - BackgroundWriteProcess - An object representing the state of this operation. @see BackgroundWriteProcess
bgwrite_chunk(fileObj, data, chunkSize, closeWhenFinished=False, chainAfter=None, ioPrio=4)
bgwrite_chunk - Chunk up the data into even #chunkSize blocks, and then pass it onto #bgwrite.
    Use this to break up a block of data into smaller segments that can be written and flushed.
    The smaller the chunks, the more interactive (recipient gets data quicker, iowait goes down for you) at cost of throughput.
 
    bgwrite will automatically chunk according to the given ioPrio, but you can use this for finer-tuned control.
 
    @see bgwrite
 
@param data <string/bytes> - The data to chunk up
 
@param chunkSize <integer> - The max siZe of each chunk.
nonblock_read(stream, limit=None, forceMode=None)
nonblock_read - Read any data available on the given stream (file, socket, etc) without blocking and regardless of newlines.
 
    @param stream <object> - A stream (like a file object or a socket)
    @param limit <None/int> - Max number of bytes to read. If None or 0, will read as much data is available.
    @param forceMode <None/mode string> - Default None. Will be autodetected if None. If you want to explicitly force a mode, provide 'b' for binary (bytes) or 't' for text (Str). This determines the return type.
 
    @return <str or bytes depending on stream's mode> - Any data available on the stream, or "None" if the stream was closed on the other side and all data has already been read.

 
Data
        __all__ = ('nonblock_read', 'bgwrite', 'bgwrite_chunk', 'BackgroundIOPriority', 'bgread')