nonblock (version 3.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, priorityPct, charityRate=1.85, charityTime=0.0003)
__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 priorityPct - integer > 0, generally 0-100. When this number is high, throughput for the operation will be higher. When it is lower,
   interactivity is higher, e.x. if you have a calculation going and a write going, the lower this number the longer the write will take, but the more
   calculations will be performed during that period.
 
@param charityRate - float >= 0, Every couple of blocks written, the current throughput is checked and if things have been going swiftly
   a short sleep will be incurred. Increasing this number causes that check to happen more often.
 
   This number is related to both the number of blocks and the priorityPct. The default, should be fine, but you may find it better
   as a different value in certain cases. Increasing or decreasing could either increase or decrease interactivity, depending on those other factors.
   Generally, however, increasing this increases interactivity and ability to write in parallel, at the cost of throughput.
 
@param charityTime - float >= 0 - Used to calculate the time to sleep when the charity period hits. The equation is:
    sleepTime = charityTime * ((dataWritten / delta) / ( (dataWritten / delta) * priorityPctDec))
     Where dataWritten = number of bytes written already, delta = total time spent writing (not including charity time sleeping)
     and priorityPctDec = priorityPct / 100.
 
     Increasing this can increase interactivity and allow more parallel operations at the cost of throughput.
     The default should be fine for the majority of cases, but it is tunable.
 
An "interactivity score" is defined to be (number of calculations) / (time to write data).
__setitem__(self, key, value)

Data descriptors defined here:
chainPollTime
charityRate
charityTime
defaultChunkSize
priorityPct

 
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')