Python通过win32com运行MessageQueue.Peek,如何正确设置超时时间?

Int*_*rer 9 python msmq msmq-wcf win32com pythoncom

首先,我想说如果有人可以在这里提供帮助,您真是不可思议。

一般问题

我的Python程序需要与MSMQ进行交互。基本上,我想窥视一个队列,如果队列中没有任何内容,则指定一个超时。

但是,尽管我已尽力而为,但当队列中先前没有任何值时,我无法让Peek()等待超时间隔。 您能否指出此代码中缺少的内容?


我当前的代码

现在是我的代码:

from socket import gethostname

import win32com.client
import pythoncom

import clr
clr.AddReference("System")
clr.AddReference("System.Messaging")
from System import TimeSpan
from System.Messaging import MessageQueue


# Source: [1]
# [1] https://docs.microsoft.com/en-us/previous-versions/windows/desktop/msmq/ms707027%28v%3dvs.85%29
MQ_DENY_NONE = 0x0
MQ_PEEK_ACCESS = 0x1
MQ_SEND_ACCESS = 0x2


# Set up queue
pythoncom.CoInitialize()
qinfo = win32com.client.Dispatch("MSMQ.MSMQQueueInfo")
qinfo.FormatName = f"direct=os:{gethostname()}\\PRIVATE$\\MyQueue"
queue = qinfo.Open(MQ_PEEK_ACCESS, MQ_DENY_NONE)

# Receive a value
timeout_sec = 1.0
timespan = TimeSpan.FromSeconds(timeout_sec)
label, body = "", ""
# TODO: timeout value does not appear working. It never waits when
#  there's no message
if queue.Peek(pythoncom.Empty, pythoncom.Empty, timespan):
    msg = queue.Receive() . # Blocking receive --> remove msg from the queue
    if msg is not None:
        label = msg.Label
        body = msg.Body
Run Code Online (Sandbox Code Playgroud)

我运行:inspect.getfullargspec(queue.Peek)并得到:

FullArgSpec(args=['self', 'WantDestinationQueue', 'WantBody', 'ReceiveTimeout', 'WantConnectorType'], varargs=None, varkw=None, defaults=(<PyOleMissing object at 0x00000147F5D43BD0>, <PyOleMissing object at 0x00000147F5D43BD0>, <PyOleMissing object at 0x00000147F5D43BD0>, <PyOleMissing object at 0x00000147F5D43BD0>), kwonlyargs=[], kwonlydefaults=None, annotations={})
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情

这个问题ReceiveTimeout=timespan似乎并不能解决我的问题。

更换pythoncom.Emptypythoncom.Missing似乎并不工作

这个未解决的问题似乎与我的非常相似

Int*_*rer 1

在原始问题的评论中,@PeterBrittain 建议尝试仅使用:

超时的整数(以毫秒为单位)

我抽出时间尝试了一下,实际上,它成功了!我发现了float同样有效的价值观。以下是一些示例 Python 代码:

timeout_sec = 1.0
queue.Peek(pythoncom.Empty, pythoncom.Empty, timeout_sec * 1000):
Run Code Online (Sandbox Code Playgroud)

谢谢@PeterBrittain!