3

通过命名管道进行异步通信

 3 years ago
source link: https://blog.lilydjwg.me/2011/10/14/asynchronized-ipc-through-named-pipe.30205.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

通过命名管道进行异步通信

本文来自依云's Blog,转载请注明。

需求是这样子的:一个程序要提供一个IPC接口,接收异步的命令。这个接口应该尽量简单,能像/proc下的文件那样通过写入数据来通信,所以我选中了命名管道。读取命名管道很简单,像普通文件那样打开然后读取就可以了。但这样做的问题是,在没有写者的时候open会阻塞。man 2 open下找到了两个标志位:O_ASYNCO_NONBLOCK。我被排在前面的O_ASYNC骗了,它只是读写时使用信号进行异步操作,open依旧阻塞。继续向后翻,才看到O_NONBLOCK,还特意注明了Neither the open() nor any subsequent operations on the file descriptor which is returned will cause the calling process to wait.

试了试,发现open并不像读写时那样在将阻塞时返回EWOULDBLOCK错误,而是返回了一个可用的文件描述符。既然文件描述符都有了,接下来自然毫无悬念地select了。完整的演示代码如下:

nonblock_fifo
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import time
import select
fd = os.open('test', os.O_NONBLOCK | os.O_RDONLY)
while True:
if not select.select([fd], [], [], 1)[0]:
print('waiting...')
else:
got = os.read(fd, 1024).decode().rstrip()
if not got:
os.close(fd)
fd = os.open('test', os.O_NONBLOCK | os.O_RDONLY)
else:
print('got', got)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK