使用命名管道时,有一种方法可以执行readlines()之类的操作

时间:2015-08-18 05:26:50

标签: python named-pipes pywin32

总体目标:我试图从python exe中读取一些进度数据来更新另一个应用程序中exe的进度

我有一个python exe会做一些事情,我希望能够将进度传达给另一个程序。基于其他几个Q& A,我已经能够让我正在运行的应用程序使用以下代码将进度数据发送到命名管道

import win32pipe
import win32file
import glob
test_files = glob.glob('J:\\someDirectory\\*.htm')
# test_files has two items a.htm and b.htm
p = win32pipe.CreateNamedPipe(r'\\.\pipe\wfsr_pipe',
                          win32pipe.PIPE_ACCESS_DUPLEX,
                          win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
                          1,65536,65536,300,None)
# the following line is the server-side function for accepting a connection
# see the following SO question and answer
""" http://stackoverflow.com/questions/1749001/named-pipes-between-c-sharp-and-python
"""
win32pipe.ConnectNamedPipe(p, None)
for each in testFiles:
    win32file.WriteFile(p,each + '\n')
#send final message
win32file.WriteFile(p,'Process Complete')
# close the connection
p.close()

简而言之,示例代码将每个全局文件的路径写入NamedPipe - 这很有用,可以很容易地扩展到更多的日志记录类型事件。但是,问题是试图弄清楚如何在不知道每条可能消息的大小的情况下读取命名管道的内容。例如,第一个文件可以命名为J:\ someDirectory \ a.htm,但第二个文件名称可以包含300个字符。

到目前为止,我用来读取管道内容的代码要求我指定一个缓冲区大小

首先建立连接

file_handle = win32file.CreateFile("\\\\.\\pipe\\wfsr_pipe",
                          win32file.GENERIC_READ | win32file.GENERIC_WRITE,
                          0, None,
                          win32file.OPEN_EXISTING,
                          0, None)

然后我一直在玩文件

data = win32file.ReadFile(file_handle,128)

这通常有效,但我真的想读,直到我点击一个换行符,在我开始阅读和换行符之间的内容做一些事情,然后重复这个过程,直到我到达一个具有过程完成的行线

我一直在努力学习如何阅读,直到找到换行符(\ n)。我基本上想要按行读取文件并根据行的内容做某事(显示行或移动应用程序焦点)。

基于@meuh提供的建议我正在更新这个因为我认为缺乏示例,如何使用管道的指导

我的服务器代码

import win32pipe
import win32file
import glob
import os

p = win32pipe.CreateNamedPipe(r'\\.\pipe\wfsr_pipe',
                          win32pipe.PIPE_ACCESS_DUPLEX,
                          win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
                          1,65536,65536,300,None)
# the following line is the server-side function for accepting a connection
# see the following SO question and answer
""" http://stackoverflow.com/questions/1749001/named-pipes-between-c-sharp-and-python
"""
win32pipe.ConnectNamedPipe(p, None)
for file_id in glob.glob('J:\\level1\\level2\\level3\\*'):
    for filer_id in glob.glob(file_id + os.sep + '*'):
        win32file.WriteFile(p,filer_id)    
#send final message
win32file.WriteFile(p,'Process Complete')

# close the connection
p.close()  #still not sure if this should be here, I need more testing
# I think the client can close p

客户端代码

import win32pipe
import win32file

file_handle = win32file.CreateFile("\\\\.\\pipe\\wfsr_pipe",
                               win32file.GENERIC_READ |
                               win32file.GENERIC_WRITE,
                               0, None,win32file.OPEN_EXISTING,0, None)
# this is the key, setting readmode to MESSAGE
win32pipe.SetNamedPipeHandleState(file_handle,
                              win32pipe.PIPE_READMODE_MESSAGE, None, None)
# for testing purposes I am just going to write the messages to a file
out_ref = open('e:\\testpipe.txt','w')

dstring = ''  # need some way to know that the messages are complete
while dstring != 'Process Complete':
    # setting the blocksize at 4096 to make sure it can handle any message I
    # might anticipate
    data = win32file.ReadFile(file_handle,4096)
# data is a tuple, the first position seems to always be 0 but need to find 
# the docs to help understand what determines the value, the second is the 
# message
    dstring = data[1]
    out_ref.write(dstring + '\n')

out_ref.close() # got here so close my testfile
file_handle.close() # close the file_handle

3 个答案:

答案 0 :(得分:2)

我没有窗户,但透过api看来你应该转换 通过CreateFile()调用后添加客户端到消息模式:

win32pipe.SetNamedPipeHandleState(file_handle,
    win32pipe.PIPE_READMODE_MESSAGE, None, None)

然后每个足够长的读取将返回单个消息,即另一个在单个写入中写入的消息。您在创建管道时已经设置了PIPE_TYPE_MESSAGE。

答案 1 :(得分:1)

您可以简单地使用将包装NamedPipe的io.IOBase实现。

class PipeIO(io.RawIOBase):
    def __init__(self, handle):
        self.handle = handle
    def read(self, n):
        if (n == 0): return ""
        elif n == -1: return self.readall()
        data = win32file.ReadFile(self.file_handle,n)
        return data
    def readinto(self, b):
        data = self.read(len(b))
        for i in range(len(data)):
            b[i] = data[i]
        return len(data)
    def readall(self):
        data = ""
        while True:
            chunk = win32file.ReadFile(self.file_handle,10240)
            if (len(chunk) == 0): return data
            data += chunk

当心:未经测试,但在修复最终的拼写错误后应该有效。

然后你可以这样做:

with PipeIO(file_handle) as fd:
    for line in fd:
        # process a line

答案 2 :(得分:1)

您可以使用msvcrt模块和open将管道转换为文件对象。

发送代码

import win32pipe
import os
import msvcrt
from io import open

pipe = win32pipe.CreateNamedPipe(r'\\.\pipe\wfsr_pipe',
                          win32pipe.PIPE_ACCESS_OUTBOUND,
                          win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
                          1,65536,65536,300,None)
# wait for another process to connect
win32pipe.ConnectNamedPipe(pipe, None)
# get a file descriptor to write to
write_fd = msvcrt.open_osfhandle(pipe, os.O_WRONLY)
with open(write_fd, "w") as writer:
    # now we have a file object that we can write to in a standard way
    for i in range(0, 10):
        # create "a\n" in the first iteration, "bb\n" in the second and so on
        text = chr(ord("a") + i) * (i + 1) + "\n"
        writer.write(text)

接收代码

import win32file
import os
import msvcrt
from io import open

handle = win32file.CreateFile(r"\\.\pipe\wfsr_pipe",
                          win32file.GENERIC_READ,
                          0, None,
                          win32file.OPEN_EXISTING,
                          0, None)
read_fd = msvcrt.open_osfhandle(handle, os.O_RDONLY)

with open(read_fd, "r") as reader:
    # now we have a file object with the readlines and other file api methods
    lines = reader.readlines()

print(lines)

一些笔记。

  • 我只用python 3.4测试过这个,但我相信你可能正在使用python 2.x。
  • 如果你试图关闭文件对象和管道,Python似乎很奇怪......所以我只使用了文件对象(通过使用with块)
  • 我只创建了一端读取的文件对象,另一端写入。您当然可以使文件对象双工
    • 使用read_fd标志创建文件描述符(write_fdos.O_RDWR
    • "r+"模式而非"r""w"
    • 创建文件对象
    • 使用win32pipe.PIPE_ACCESS_DUPLEX标志
    • 创建管道
    • 返回使用win32file.GENERIC_READ | win32file.GENERIC_WRITE标志创建文件句柄对象。