社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

C到Python管道-如何检测读卡器访问

Noel • 3 年前 • 1299 次点击  

我正在用一个C程序向一个命名管道写入数据,用Python程序读取数据。

如果我停止Python程序(reader),那么编写器将自行停止,尽管这是一个while(1)循环。为什么会这样?是无声的撞击吗?

第二个问题:如果我想检测到读卡器何时断开连接,我该怎么办。我的理想情况是检测到断开连接,然后继续空闲(即停止发送任何内容),并在读卡器返回后恢复。

玩具代码如下。

作者(C):

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
  
int main()
{
    int fd;
  
    // FIFO file path
    char * myfifo = "/tmp/myfifo";
  
    // Creating the named file(FIFO)
    // mkfifo(<pathname>, <permission>)
    mkfifo(myfifo, 0666);
  
    char arr1[80];
    while (1)
    {
        // Open FIFO for write only
        fd = open(myfifo, O_WRONLY);
  
        // Take an input from user.
        fgets(arr1, 80, stdin);
  
        // Write the input on FIFO
        // and close it
        write(fd, arr1, strlen(arr1)+1);
        close(fd);
    }
    return 0;
}

阅读器(Python)

f = open("/tmp/myfifo")
while 1:
    print(f.readline(), end = "")

f.close()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131998
 
1299 次点击  
文章 [ 1 ]  |  最新文章 3 年前