Skip to main content

Introduction

txZMQ allows to integrate easily 0MQ sockets into Twisted event loop (reactor).

txZMQ supports both CPython and PyPy.

Improvement

Original API design of txZMQ was wrongly designed, and they are not reusable or hard to use, for example, to set HWM of a ZeroMQ socket, you need to change the class level variable.:

from txzmq import ZmqConnection
ZmqConnection.highWaterMark = 100
conn = ZmqConnection(factory)

As you can see the highWaterMark variable affects all connection made afterward. This is obviously not a good design.

Also, the encapsulation of endpoints is not necessary. Sometimes you may need to connect or bind a address after a ZmqConnection was created, original design has no obvious way to do it. You need to pass endpoints when create a new ZmqConnection.

PUSH/PULL and PAIR connections are not present in original version. I add those connection types in this library.

There is also a serious bug in original version. When read signal of FD is triggered before reactor perform another select, then txZMQ stop reading from that socket anymore. This version fixes the bug.

Requirements

Non-Python library required:

  • 0MQ library >= 2.1 (heavily tested with 2.1.4)

Python packages required:

  • pyzmq (for CPython)

  • pyzmq-ctypes (for PyPy)

  • Twisted

Details

txZMQ introduces support for general 0MQ sockets by class ZmqConnection that can do basic event loop integration, sending-receiving messages in non-blocking manner, scatter-gather for multipart messages.

txZMQ uses ØMQ APIs to get file descriptor that is used to signal pending actions from ØMQ library IO thread running in separate thread. This is used in a custom file descriptor reader, which is then added to the Twisted reactor.

From this class, one may implement the various patterns defined by 0MQ. For example, special descendants of the ZmqConnection class, ZmqPubConnection and ZmqSubConnection, add special nice features for PUB/SUB sockets.

Request/reply pattern is achieved via XREQ/XREP sockets and classes ZmqXREQConnection, ZmqXREPConection.

Push/pull pattern is achieved via classes ZmqPushConnection, ZmqPullConection.

Finally, Pair pattern is achieved via classes ZmqPairConnection.

Example

Here is an example of using txZMQ:

import os
import sys
import time
from optparse import OptionParser

from twisted.internet import reactor

import zmq
from txzmq import ZmqFactory, ZmqPubConnection, ZmqSubConnection

parser = OptionParser("")
parser.add_option("-m", "--method", dest="method", help="0MQ socket connection: bind|connect")
parser.add_option("-e", "--endpoint", dest="endpoint", help="0MQ Endpoint")
parser.add_option("-M", "--mode", dest="mode", help="Mode: publisher|subscriber")
parser.set_defaults(method="connect", endpoint="epgm://eth1;239.0.5.3:10011")

(options, args) = parser.parse_args()

zf = ZmqFactory()

def bind_or_connect(s):
    if options.method == 'bind':
        s.bind(options.endpoint)
    elif options.method == 'connect':
        s.connect(options.endpoint)

if options.mode == "publisher":
    pub = ZmqPubConnection(zf)
    bind_or_connect(pub)

    def publish():
        data = str(time.time())
        print "publishing %r" % data
        pub.send(data)

        reactor.callLater(1, publish)

    publish()
else:
    def doPrint(msgs):
        print "message received: %r" % (msgs, )
    sub = ZmqSubConnection(zf, callback=doPrint)
    sub.setsockopt(zmq.SUBSCRIBE, '')
    bind_or_connect(sub)

reactor.run()

The same example is available in the source code. You can run it from the checkout directory with the following commands (in two different terminals):

examples/pub_sub.py --method=bind --endpoint=ipc:///tmp/sock --mode=publisher

examples/pub_sub.py --method=connect --endpoint=ipc:///tmp/sock --mode=subscriber

Hacking

Source code for txZMQ is available at github; forks and pull requests are welcome.

To start hacking, fork at github and clone to your working directory. To use the Makefile (for running unit tests, checking for PEP8 compliance and running pyflakes), you will want to have virtualenv installed (it includes a pip installation).

Create a branch, add some unit tests, write your code, check it and test it! Some useful make targets are:

  • make env

  • make check

  • make test

If you don’t have an environment set up, a new one will be created for you in ./env. Additionally, txZMQ will be installed as well as required development libs.

Metadata

Release files for vic-txZMQ 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vic-txZMQ 0.5.2
File Size Uploaded
vic-txZMQ-0.5.2.zip 27.8 kB Details

Release files / vic-txZMQ-0.5.2.zip

Download URL vic-txZMQ-0.5.2.zip
Size 27.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8b83ec6634b8c2a0acb65476d4b6a3ce8cb15c4d51774b14d4c69663b6562501
BLAKE2b-256 checksum
How to use checksums
ecd16c083d9a1c26576d4f478d4068e344cde91aedc54a68a997c7a00ebfa75d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.5.2 This release

1 release file

0.5.1

1 release file

0.5.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page