【发布时间】:2015-08-18 05:26:49
【问题描述】:
总体目标:我正在尝试从 python exe 中读取一些进度数据,以更新另一个应用程序中 exe 的进度
我有一个 python exe 将做一些事情,我希望能够将进度传达给另一个程序。基于此处的其他几个问答,我已经能够让我正在运行的应用程序使用以下代码将进度数据发送到命名管道
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)
这通常有效,但我真的很想阅读,直到我遇到换行符,对我开始阅读和换行符之间的内容做一些事情,然后重复这个过程,直到我到达一个有 Process Complete 的行线
在找到换行符 (\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
【问题讨论】:
-
也许你可以建立一个约定,发送进程发送一个固定长度的整数,表示行长,然后是行?
标签: python named-pipes pywin32