Skip to main content

Twisted bindings for ZeroMQ

Project description

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

Supports CPython and PyPy.

Requirements:

  • ZeroMQ library >= 2.1 (heavily tested with 2.1.4)

Python packages required:

  • pyzmq (for CPython)

  • pyzmq-ctypes (for PyPy)

  • Twisted

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

Special descendants of that class, ZmqPubConnection and ZmqSubConnection add special nice features for PUB/SUB sockets.

Other socket types could be easily derived from ZmqConnection except for REQ/REP sockets that may require more work, as reply should be send immediately upon receiving message (currently ZmqConnection will try to read all available message and write queued messages in parallel).

Example:

import sys

from optparse import OptionParser

from twisted.internet import reactor, defer

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()

from txZMQ import ZmqFactory, ZmqEndpoint, ZmqPubConnection, ZmqSubConnection
import time

zf = ZmqFactory()
e = ZmqEndpoint(options.method, options.endpoint)

if options.mode == "publisher":
    s = ZmqPubConnection(zf, e)

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

        reactor.callLater(1, publish)

    publish()
else:
    s = ZmqSubConnection(zf, e)
    s.subscribe("")

    def doPrint(*args):
        print "message received: %r" % (args, )

    s.gotMessage = doPrint

reactor.run()

The same example is available in source code:

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, please install virtualenv and pip. In fresh checkout, run:

make env

(If your virtualenv binary has different name, you can specify it via make variables: make env VIRTUALENV=virtualenv-2.7)

This should make new virtual environment at env/ and install txZMQ and development requirements.

Run tests and style checks:

make

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

txZMQ-0.2.tar.gz (13.0 kB view details)

Uploaded Source

File details

Details for the file txZMQ-0.2.tar.gz.

File metadata

  • Download URL: txZMQ-0.2.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for txZMQ-0.2.tar.gz
Algorithm Hash digest
SHA256 e66848babf6f912b01645d0cf7c692821386a4c87f3e2a45c468446dca6c59e8
MD5 81c82f0cc5e7e541f36bc2927fc9ac4c
BLAKE2b-256 7adcf46030d77199abf6f7d8b1635ffb3390121b1c00cf6e1f2a9c47f0f08f5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page