Python - 非阻塞写入subprocess.PIPE?

时间:2014-03-08 00:11:12

标签: python stdin nonblocking

我正在使用python开发Raspberry Pi视频应用程序。我修改了一个播放全高清电影的c程序来从FIFO中获取输入。

像这样:

$ ./hello_video.bin test.h264 < .my_fifo
$ echo 3 > .my_fifo

我可以向FIFO发送号码,hello_video.bin将触发相应的视频片段并永久循环。

下一步应该是让视频可以从远程触发。这就是我所拥有的:

#!/usr/bin/env python
import socket
import threading
import Queue
import subprocess
import sys
from time import sleep

host = ''       # empty port = all local ports
port = 5005     
buffer = 2      # small buffer sufficient

player = "/opt/vc/src/hello_pi/hello_video/hello_video.bin"
clip = "/opt/vc/src/hello_pi/hello_video/test.h264"

p = subprocess.Popen([player, clip], stdin=subprocess.PIPE)

class readFromUDPSocket(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        while True:
            data,addr = socketUDP.recvfrom(buffer)
            try:
                p.stdin.write(data)
                p.stdin.flush() 

            except:
                pass   


if __name__ == '__main__':

    # Create socket (IPv4 protocol, datagram (UDP)) and bind to address
    socketUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    socketUDP.bind((host, port))

    # Instantiate & start threads
    myServer = readFromUDPSocket()
    myServer.daemon = True
    myServer.start()

while 1:
    pass

UDPSock.close()

不幸的是,hello_video.bin没有按原样做出反应。这些命令不会触发任何其他视频。

我尝试使用p.communicate(input = data)。这有效,但只有一次。然后它阻塞,我无法从套接字接收任何其他数据。

我不知道如何解决这个问题 - 希望你能帮到我, 谢谢

0 个答案:

没有答案