Using MSMQ from IronPython

Trying out MSMQ from Python is a pain as it is from unmanaged code. But, by using python you get the benefits of using the System.Messaging namespace with the flexibility of dynamic code. Here is how you can do this.

Start IronPython by

ipy.exe -X:TabCompletion -X:ColorfulConsole -X:ExceptionDetail

>>> import clr
>>> clr.AddReference('System.Messaging')
>>> from System.Messaging import *
>>> queue = MessageQueue('.\\private$\\myqueue')
>>> queue.Send('Hello from IronPython')

Screenshot:


There it is, your message in MSMQ. Note this code assumes that you already have a queue with that name. If you dont have it, then lets create it now.

# First store the queue name is a string, so that we dont need to type it again
>>> targetQueue = '.\\private$\\myqueue'

# Lets check if it exists or not
>>> MessageQueue.Exists(targetQueue)
False

# Ok, create one now
>>> MessageQueue.Create(targetQueue)
<System.Messaging.MessageQueue object at ........>

Now you can send it by using the process above.
You have to try this out to see how cool is IronPython and its parent Python.

1 Comment

  • Thats really quite cool - I've been looking at IronPython and am rapidly realising it brings an awful lot to the command line. Certainly fills the gaps where PowerShell (and its haenous syntax) fall down.
    Regards, Nigel

Comments have been disabled for this content.