纯python非阻塞io函数

python-nonblock的Python项目详细描述


非阻塞python io函数

这些是纯python函数,在python中执行非阻塞i/o。

nonblock_read

nonblock_read提供以非阻塞方式读取缓冲区上可用的任何内容(如文件、管道或套接字)的能力。readline等方法将被阻塞,直到打印换行符等。

您可以提供一个限制(或者默认的“无”是任何可用的),如果可用的话,最多可以返回这么多字节。

当流在另一端关闭,并且您已经读取了所有数据(即您已经返回了所有数据,并且将来不可能有更多的数据)时,将返回“无”。

def 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 - 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.

''

请记住,只能读取从另一侧刷新的数据,否则缓冲区不存在数据。

如果需要对来自终端的sys.stdin执行非阻塞读取,则需要使用“tty.setraw(sys.stdin)”将其置于raw模式。有关示例,请参见examples/simplegame.py。

示例用法:

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数据:

# Some data has been read, process it

processData(data)

其他:

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

idleTask()

#所有数据都已处理完毕,请关注空闲任务

idletask()

一个简单的示例游戏,使用非块读取驱动输入,同时总是刷新地图和移动怪物,可以在:https://github.com/kata198/python-nonblock/blob/master/example/simpleGame.py

后台读取-bgread

有时您可能希望从后台的一个或多个流中收集数据,并在稍后检查/处理数据。

python nonblock通过一个方法“bgread”提供此功能。您提供一个stream对象和选项,它输出一个对象,当数据在流中可用时,该对象将由线程在后台自动填充。

‘’‘

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)从流中读取所有可用数据后,请等待几秒钟,然后再次检查更多数据。

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>;-默认为true。如果为true,则当另一侧已关闭且已读取所有数据时,将调用流对象上的“close”方法。

注释–

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)

使用默认值.03和65535意味着您将读取高达每秒2 MB的数据。请记住,在I/O中花费的时间越多,意味着花在其他任务上的时间就越少。

@return-此函数的返回是一个backgroundreaddata对象。此对象包含属性“blocks”,该属性是从流中读取的非零长度块的列表。对象还包含一个计算属性“data”,它是当前读取的所有数据的字符串/字节(取决于流模式)。流关闭后,属性“isfinished”将设置为true。属性“error”将设置为读取期间发生的任何异常,该异常将终止线程。@有关详细信息,请参阅backgroundreaddata。

''

例如:

inputData = bgread(sys.stdin)

processThings() # Do some stuff that takes some time

typedData = inputData.data # Get all the input that occured during ‘processThings’.

后台写入-bgwrite

python nonblock提供了一种以非阻塞、可配置和交互式支持方式写入流的干净方法。

此功能的核心来自bgWrite函数:

def 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>;-如果为true,则在写入所有数据后将关闭给定的fileobj。默认为False。

@param chainafter<;none/backgroundriteprocess>;-如果提供了backgroundriteprocess对象(返回bgwrite*函数),则在与提供的对象关联的数据完成写入之前,将保留此数据以供写入。

使用此选项可对多个后台写入进行排队,但保留结果流中的顺序。

@return-backgroundriteprocess-一个表示g此操作的状态。@请参见backgroundriteprocess

''

您可以使用“chainAfter”参数创建要写入给定流的数据队列,提供前一个“bgWrite”或“bgWrite_chunk”函数的返回。这将等待上一个bgwrite完成,然后再开始下一个bgwrite。

bwrite将以块的形式写入数据并执行启发式,以便根据预定义的backgroundiopriority为其他正在运行的线程和计算提供交互性,或者您可以提供自定义的backgroundiopriority(有关参数,请参阅下面的“完整文档”。

示例

除了执行CPU绑定计算外,还使用多个bgwrite的脚本示例可以在以下位置找到:https://github.com/kata198/python-nonblock/blob/master/testWrite.py

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java限制C++代码访问JNI中的某些类   Android上的java DateFormat:不可解析的日期   通过json进行java迭代,并为其他请求调用多个API   Netbeans中的java JavaFX项目引发异常“输入流不能为null”   多线程Java newFixedThreadPool解释   |在java字符串中无法识别。split()方法   Java中的原始包装器类是否被视为引用类型?   Java swing。如何在intellij idea GUI设计工具中重写组件方法   数组乘矩阵   java将30GB的XML文件分割成小块XML   java通过一棵树递归找到一个节点,并返回指向该节点的路径   java如何将可观察的<Observable<List<T>>转换为可观察的<List<T>>   使用java在web服务器上更改php文件中的字符串?   java希望开发像tomcat这样的servlet容器   java希望提高编程的数学技能