Skip to main content

Pure-Python non-blocking IO functions

Project description

Non-blocking python IO functions

These are pure-python functions which perform non-blocking I/O in python.

nonblock_read

nonblock_read provides the ability to read anything available on a buffer, like a file or a pipe or a socket, in a non-blocking fashion. Methods like readline will block until a newline is printed, etc.

You can provide a limit (or default None is anything available) and up to that many bytes, if available, will be returned.

When the stream is closed on the other side, and you have already read all the data (i.e. you’ve already been returned all data and it’s impossible that more will ever be there in the future), “None” is returned.

def nonblock_read(stream, limit=None, forceMode=None):

‘’’

nonblock_read - Read any data available on the given stream without blocking and regardless of newlines.

@param stream - A stream (like a file object)

@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> - If the stream object doesn’t specify a “mode” param (like a socket), this function will assume the encoding as “bytes”.

If you want to force a stream mode, use “t” for text (str), or “b” for binary (bytes). Usually not required.

@return - Any data available on the stream, or “None” if the stream was closed on the other side and all data has already been read.

‘’’

An example usage:

from nonblock import nonblock_read

pipe = subprocess.Popen([‘someProgram’], stdout=subprocess.PIPE)

while True:

data = nonblock_read(pipe.stdout)

if data is None:

# All data has been processed and subprocess closed stream

pipe.wait()

break

elif data:

# Some data has been read, process it

processData(data)

else:

# No data is on buffer, but subprocess has not closed stream

idleTask()

# All data has been processed, focus on the idle task

idleTask()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-nonblock-1.0.0.tar.gz (3.3 kB view details)

Uploaded Source

File details

Details for the file python-nonblock-1.0.0.tar.gz.

File metadata

File hashes

Hashes for python-nonblock-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5c18223698712ce2e3d51ad82173111909a83026a0f5c898d726dbad841ef76d
MD5 725aa736104b6c4c98c70860e57aa8e2
BLAKE2b-256 8e0b44dc3dbdb70beb801f56a90eb8a64546928cab8cdc1cad17c8abd87208f3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page