作者有没有办法知道读者已经closures了命名pipe道(或退出)的末尾, 而不写信给它?
我需要知道这一点,因为我写入pipe道的初始数据是不同的; 在数据的其他部分出现之前,读者正在等待一个初始标题。
目前,当我的write()与EPIPE失败时,我检测到这一点。 然后我设置一个标志,说“下一次,发送标题”。 但是,在我写任何东西之前,读者可以closures并重新打开pipe道。 在这种情况下,我从来没有意识到他做了什么,也不会发送他期待的头球。
有没有什么asynchronous事件types的东西可以帮助这里? 我没有看到任何信号被发送。
Bash到批量转换
os.fork()会在写入时使用copy还是在Python中完成父进程的完整副本?
Bash数组创build:(“$ @”)vs($ @)
在terminal窗口中启动文件pipe理器
使用相同的密钥循环访问关联数组
请注意,我没有包含任何语言标记,因为这个问题应该被认为是语言不可知的。我的代码是Python,但答案应该适用于C或其他具有系统调用级别绑定的语言。
为什么“猫a.txt | xargs vi“销毁bash?
Windows / Linux的iOS应用程序开发(编译不需要)
GCC 4.7源字符编码和执行字符编码string文字?
如果您正在使用基于poll系统调用的事件循环,则可以使用包含EPOLLERR的事件掩码来注册管道。 在Python中, select.poll ,
import select fd = open("pipe","w") poller = select.poll() poller.register(fd,select.POLLERR) poller.poll()
将等待,直到管道关闭。
要测试这个,运行mkfifo pipe ,启动脚本,并在另一个终端运行,例如cat pipe 。 一旦你退出cat进程,脚本将会终止。
奇怪的是,当最后一个阅读器关闭管道时, select显示管道是可读的:
writer.py
#!/usr/bin/env python import os import select import time NAME = 'fifo2' os.mkfifo(NAME) def select_test(fd,r=True,w=True,x=True): rset = [fd] if r else [] wset = [fd] if w else [] xset = [fd] if x else [] t0 = time.time() r,w,x = select.select(rset,wset,xset) print 'After {0} sec:'.format(time.time() - t0) if fd in r: print ' {0} is readable'.format(fd) if fd in w: print ' {0} is writable'.format(fd) if fd in x: print ' {0} is exceptional'.format(fd) try: fd = os.open(NAME,os.O_WRONLY) print '{0} opened for writing'.format(NAME) print 'select 1' select_test(fd) os.write(fd,'test') print 'wrote data' print 'select 2' select_test(fd) print 'select 3 (no write)' select_test(fd,w=False) finally: os.unlink(NAME)
演示:
1号航站楼:
$ ./pipe_example_simple.py fifo2 opened for writing select 1 After 1.59740447998e-05 sec: 3 is writable wrote data select 2 After 2.86102294922e-06 sec: 3 is writable select 3 (no write) After 2.15910816193 sec: 3 is readable
2号航站楼:
$ cat fifo2 test # (wait a sec,then Ctrl+C)
没有这样的机制。 一般情况下,根据UNIX方式,任何一端都没有流打开或关闭的信号。 这只能通过阅读或写入来检测(相应地)。
我会说这是错误的设计。 目前,您正尝试让接收机通过打开管道来显示其可用性。 所以要么以适当的方式实现这个信号,要么在管道的发送部分中包含“关闭逻辑”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。