Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中执行C stdio.h read()_Python - Fatal编程技术网

如何在Python中执行C stdio.h read()

如何在Python中执行C stdio.h read(),python,Python,我有一个专有程序,可以执行用户提供的程序作为子进程。父进程将一些有用的数据写入文件描述符4 在C程序中,可以使用以下方法读取文件描述符中的数据: s_count=read(4,&string_var[0],100) 我正试图用Python实现同样的目标 以下内容不起作用,并在读取时挂起 stream = os.fdopen(4, "r") stream.read() 同样的问题 os.read(4, 100) 我甚至尝试将文件描述符更改为非阻塞 有什么方法可以像Python中的Cstdio.

我有一个专有程序,可以执行用户提供的程序作为子进程。父进程将一些有用的数据写入文件描述符4

在C程序中,可以使用以下方法读取文件描述符中的数据:

s_count=read(4,&string_var[0],100)

我正试图用Python实现同样的目标

以下内容不起作用,并在读取时挂起

stream = os.fdopen(4, "r")
stream.read()
同样的问题

os.read(4, 100)
我甚至尝试将文件描述符更改为非阻塞

有什么方法可以像Python中的
C
stdio.h
read()
那样做呢

==============================================

编辑-已解决

程序在不同的文件描述符上有另一个可写流。 在更改无缓冲输入流(即,它是打开的)后,问题得到了解决。将
0
作为第三个参数传递到
os。python open()中的fdopen
返回一个文件描述符,最常用于两个参数:open(filename,mode)


我必须让它成为非阻塞的,然后使用selectpoll

fcntl.fcntl(4, fcntl.F_SETFL, os.O_NONBLOCK)
poller = select.poll()
poller.register(o, select.POLLIN)
if poller.poll():
    print(o.read())

参考python documentation.BTW中的主题,假设文件描述符有效,标准的
open
函数将接受文件描述符而不是文件名。
fcntl.fcntl(4, fcntl.F_SETFL, os.O_NONBLOCK)
poller = select.poll()
poller.register(o, select.POLLIN)
if poller.poll():
    print(o.read())