- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的问题是:select
表示有数据要读,有什么就读什么,不想等max
存在的金额。如果 max
<= 0 然后读取等待直到遇到 EOF,如果 max
>0 读取块直到 max
字节可以读取。
我不想要这个,我想阅读任何数量的 select 把它放在“准备阅读”列表中。 read(1) 是不切实际的,因为这将涉及到大量的读取调用。但它不能阻塞。
有没有办法在 select 返回时找出缓冲区中存在的数量(如果它返回表明可以读取某些内容,而不是超时)并读取该数量?有没有办法使用max
就像使用套接字一样?它立即读取尽可能多的内容,然后返回?
解决方案可能是将文件置于非阻塞模式以进行读取?我不确定,我没想到这种“直到 EOF”的行为。
我会继续阅读和尝试,但我只花了 30 分钟左右的时间却没有接近,这就是我呼吁你的原因。
注意
有很多问题询问如何让 recv 等待一定数量的输入,并使事情阻塞直到达到最大值,我是 不是 寻找这个。我的问题是它被阻塞了。
附录 setblocking(False)
没有用,我现在正在阅读如何使其在读取期间不阻塞。文档给了我希望:
stdin.read Found at: sys
read([size]) -> read at most size bytes, returned as a string.
If the size argument is negative or omitted, read until EOF is reached.
Notice that when in non-blocking mode, less data than what was
requested
may be returned, even if no size parameter was given.
def getInput(self):
log.log(log.INFO,"GetInput","Select")
readsReady = select.select((sys.stdin,),(),(),1)[0]
if len(readsReady) == 0:
#timed out
log.log(log.INFO,"GetInput","Select timed out")
if not self.toClose:
self.handler.post("GetInput")
else:
threads.getCurrentThread().removeAllHandlers()
else:
#OPTIMISED FOR READING 1
#log.log(log.INFO,"GetInput","Reading")
data = sys.stdin.read(1)
log.log(log.INFO,"GetInput","Read: "+data)
if data == "\n":
self.onInputHandler.post("OnInput",self.buffer)
self.buffer=""
else:
self.buffer+=data
self.handler.post("GetInput")
0.0147 Verbose 1 SocketReader Created reader
0.0156 Verbose 2 SocketWriter Created writer
0.0260 Information 0 SocketReadWriter Created and ready for: ('localhost', 8294)
0.0268 Information 3 GetInput Select
Hello World!
1.0281 Information 3 GetInput Select timed out
1.0584 Information 3 GetInput Select
2.0593 Information 3 GetInput Select timed out
2.0896 Information 3 GetInput Select
3.0900 Information 3 GetInput Select timed out
3.1203 Information 3 GetInput Select
4.1215 Information 3 GetInput Select timed out
4.1519 Information 3 GetInput Select
TEST!
5.1524 Information 3 GetInput Select timed out
5.1828 Information 3 GetInput Select
hello
6.1467 Information 3 GetInput Read: h
6.1770 Information 3 GetInput Select
7.1782 Information 3 GetInput Select timed out
7.2086 Information 3 GetInput Select
8.2098 Information 3 GetInput Select timed out
8.2401 Information 3 GetInput Select
9.2414 Information 3 GetInput Select timed out
9.2717 Information 3 GetInput Select
10.2723 Information 3 GetInput Select timed out
10.3026 Information 3 GetInput Select
k
10.7939 Information 3 GetInput Read: e
10.8243 Information 3 GetInput Select
10.8245 Information 3 GetInput Read: l
10.8547 Information 3 GetInput Select
10.8549 Information 3 GetInput Read: l
10.8851 Information 3 GetInput Select
10.8853 Information 3 GetInput Read: o
10.9155 Information 3 GetInput Select
10.9157 Information 3 GetInput Read:
10.9459 Information 3 GetInput Select
10.9461 Information 3 GetInput Read: k
10.9763 Information 3 GetInput Select
You said: hello
11.9775 Information 3 GetInput Select timed out
12.0123 Information 3 GetInput Select
13.0133 Information 3 GetInput Select timed out
13.0437 Information 3 GetInput Select
^C13.3985 Verbose 2 Threads Thread: 2 has ended
14.0442 Information 3 GetInput Select timed out
14.0746 Information 3 GetInput Select
14.3622 Verbose 1 Threads Thread: 1 has ended
15.0758 Information 3 GetInput Select timed out
15.1363 Information 3 GetInput Select
16.1373 Information 3 GetInput Select timed out
16.1677 Verbose 3 Threads Thread: 3 has ended
def getInput(self):
log.log(log.INFO,"GetInput","Select")
if sys.stdin.closed:
readsReady = []
else:
readsReady = select.select((sys.stdin,),(),(),1)[0]
if len(readsReady) == 0:
#timed out
log.log(log.INFO,"GetInput","Select timed out")
if not self.toClose:
self.handler.post("GetInput")
else:
threads.getCurrentThread().removeAllHandlers()
else:
data = sys.stdin.readline()
if len(data) == 0:
log.log(log.WARN,"GetInput","No data was returned indicating the file was closed")
self.handler.post("GetInput") #if this is a close event, the next
#timeout will deal with it
return
if data[-1] == "\n":
data = data[:-1]
log.log(log.INFO,"GetInput","Read: "+data)
self.onInputHandler.post("OnInput",data)
#if data == "\n":
# self.onInputHandler.post("OnInput",self.buffer)
# self.buffer=""
#else:
# self.buffer+=data
self.handler.post("GetInput")
def onClose(self):
#log.log(log.WARN,"Input: OnClose","Called")
self.toClose = True
sys.stdin.close()
最佳答案
在 os
模块有 os.read
允许较低级别控制从文件描述符读取的函数。只要至少有一个字节准备好读取,它就是非阻塞的。
os.read(fd, n)
Read at most n bytes from file descriptor fd. Return a string containing the bytes read. If the end of the file referred to by fd has been reached, an empty string is returned.
Availability: Unix, Windows.
Note: This function is intended for low-level I/O and must be applied to a file descriptor as returned by
os.open()
orpipe()
. To read a “file object” returned by the built-in functionopen()
or bypopen()
orfdopen()
, orsys.stdin
, use itsread()
orreadline()
methods.
关于Python sys.stdin.read(max) 阻塞直到读到max(如果max>=0),阻塞直到EOF else,但是select表示有数据要读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18937338/
我有一个阅读器,其中包含有关 51*51 网格的信息,其中网格上的每个点都由 f32 表示。 .我想将这些数据读入一个向量,以便我可以轻松处理它: pub fn from_reader(reader:
我重新启动了 SQL Server 2005 并运行了统计 IO 的查询。 我得到了这些结果:表“xxx”。扫描计数 1,逻辑读取 789,物理读取 3,预读读取 794,... 预读读取数是读取并放
在 CLHS 中,我为 :read-only x 读到:“当 x 为真时,这指定不能更改此插槽;它将始终包含构造时提供的值。” 我可以做到这一点(CCL、SBCL): CL-USER> (defstr
让我们考虑一下这句话(Total Store Ordering): reads are ordered before reads, writes before writes, and reads be
我正在开发一个 SMTP 库,它使用缓冲读取器通过网络读取行。 我想要一种安全的方式来从网络读取数据,而不依赖于 Rust 内部机制来确保代码按预期工作。具体来说,我想知道 Read trait 是否
我不清楚所有这些读取字符串函数之间的关系。嗯,很明显clojure.core/read-string可以读取 pr[n] 输出的任何序列化字符串甚至 print-dup .也很清楚clojure.ed
所以我做了这个功能,就像倒计时一样。我想在倒计时减少时读取命令。我的大问题是让 read() 在倒计时减少时等待输入。如您所见,我尝试使用 select() 但在第一个 printf 之后("time
这是我vue3+echart5 遇到的报错:Cannot read properties of undefined (reading ‘type‘) 这个问题需要搞清楚两个关键方法: toRaw: 作
下图中,左边是C代码,右边是未优化的LLVM IR形式。 The Figure 在 IR 上运行 MemoryDependenceAnalysis 可查找内存依赖性。原始代码及其 IR 等效代码中
这个问题在这里已经有了答案: Read values into a shell variable from a pipe (17 个答案) 关闭 3 年前。 我一直在尝试像这样从程序输出中读取环境变
当我输入相同的整数时,如何将整数转换为与使用 read(0,buff,nbytes) 获得的缓冲区相同的值/编码字符?我正在尝试编写类似 read() 的东西,但用整数数据代替读取到缓冲区的文件描述符
This question already has answers here: Closed 2 years ago. Read input in bash inside a while loop (
我正在尝试处理来自 MySQL 数据库的一些数据(主要是 double 值)。我收到此错误消息: Invalid attempt to access a field before calling Re
我正在制作一个简单的 TCP/IP 套接字应用 这样做有什么不同: DataInputStream in = new DataInputStream(clientSocket.getInputStre
我操作API服务器。 手机APP访问API服务器时,有时会出现该异常。 我尝试在测试服务器上进行测试,但无法重现。(我改变了apache和tomcat的连接时间。) 有什么问题?? 我该如何解决这个问
我在段落末尾使用“阅读更多”只是为了提醒像P.T.O一样的用户 为什么会有问题? 最佳答案 您必须明白,许多屏幕阅读器用户不会等到整个页面都读给他们听。他们使用键盘快捷键在页面中导航。 JAWS(可以
我已将我的 Angular 应用程序从 12 版本升级到 13 版本。我在单元测试运行期间开始遇到此错误。 Chrome Headless 94.0.4606.61 (Windows 10) AppC
我正在尝试为以下组件编写一个。我正在使用 queryParams 然后使用 switchmap 来调用服务。这是 url 的样子: http://localhost:4200/test-fee/det
我的代码有什么问题? Uncaught TypeError: Cannot read properties of undefined (reading 'remove') 和 Uncaught Typ
我在我的 React 应用程序中遇到了这个问题。 类型错误:无法读取未定义的属性(读取“requestContent”) 我在我的应用程序中使用 commercejs。代码指向 isEmpty=!ca
我是一名优秀的程序员,十分优秀!