python c++ 交互之管道 pipe
2017-03-16 11:09
1591 查看
linux 下 python c++ 使用管道进行数据通信示例
c++ code writer.cpp
Python code reader.py
运行效果:
c++ code writer.cpp
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #define _PATH_ "/tmp/vsd_fifo.pipe" #define _SIZE_ 100 void Write(){ int ret = mkfifo(_PATH_, 0666 | S_IFIFO); if (ret == -1){ printf("Failed to create pipe\n"); //return; } int fd = open(_PATH_, O_WRONLY); if(fd <0){ printf("Failed to open\n"); return; } char buf[_SIZE_]; while(1){ printf("Please input: \n"); scanf("%s", buf); int ret = write(fd, buf, strlen(buf) + 1); if (ret < 0){ printf("Write error"); break; } } close(fd); } int main(){ Write(); return 1; }
Python code reader.py
import os import time read_path = "/tmp/vsd_fifo.pipe" try: os.mkfifo(read_path) except OSError, e: print "mkfifo error", e rf = os.open(read_path, os.O_RDONLY) print("os.open finished") while True: data = os.read(rf, 1024) if len(data) == 0: print("nodata") time.sleep(1) print data
运行效果:
相关文章推荐
- Python 3 利用 subprocess 实现管道( pipe )交互操作读/写通信
- Python 3 利用 subprocess 实现管道( pipe )交互操作读/写通信
- Python 与 C++ 的交互编程
- c++ python交互之boost.python 简集之普通函数
- Python的subprocess子进程和管道进行交互
- C++程序员学Python:C与Python进行交互
- Python的subprocess子进程和管道进行交互
- Python 与 C++ 的交互编程
- python利用管道popen调用.exe进行交互
- C++ python 交互之 python.dict to c++ map
- c++ python交互之boost.python 简集之Vector(map)混合使用
- Python的subprocess子进程和管道进行交互
- c++ python交互之boost.python 简集之普通函数
- c++ python交互之boost.python 简集之Map处理
- c++ python交互之boost.python 简集之类成员变量设置
- C++ python 交互之 python.dict to c++ map
- python类库31[进程subprocess与管道pipe]
- c++ python交互之boost.python 简集之类成员变量设置
- c++ python交互之boost.python 简集之 vector处理
- Python 与 C++ 的交互编程