{ "info": { "author": "Jakub Matys", "author_email": "matys.jakub@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Framework :: Twisted", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation :: CPython", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "txmsgpackrpc\n============\n\n.. image:: https://travis-ci.org/jakm/txmsgpackrpc.svg?branch=master\n :target: https://travis-ci.org/jakm/txmsgpackrpc\n\nFor the latest source code, see http://github.com/jakm/txmsgpackrpc\n\n``txmsgpackrpc`` is a library for writing asynchronous\n`msgpack-rpc `__\nservers and clients in Python, using `Twisted\nframework `__. Library is based on\n`txMsgpack `__, but some\nimprovements and fixes were made.\n\nFeatures\n--------\n\n- user friendly API\n- modular object model\n- working timeouts and reconnecting\n- connection pool support\n- TCP, SSL, UDP and UNIX sockets\n\nPython 3 note\n-------------\n\nTo use UNIX sockets with Python 3 please use Twisted framework 15.3.0 and above.\n\nDependencies\n------------\n\n- msgpack-python https://pypi.python.org/pypi/msgpack-python/\n- Twisted http://twistedmatrix.com/trac/\n\nInstallation\n------------\n\n.. code:: sh\n\n % pip install txmsgpackrpc\n\nDebian packages are available on project's `Releases\npage `__.\n\nTCP example\n-----------\n\nComputation of PI using Chudnovsky algorithm in subprocess. For details,\nsee http://www.craig-wood.com/nick/articles/pi-chudnovsky/.\n\nResults\n~~~~~~~\n\n::\n\n Computation of PI with 5 places finished in 0.022390 seconds\n\n Computation of PI with 100 places finished in 0.037856 seconds\n\n Computation of PI with 1000 places finished in 0.038070 seconds\n\n Computation of PI with 10000 places finished in 0.073907 seconds\n\n Computation of PI with 100000 places finished in 6.741683 seconds\n\n Computation of PI with 5 places finished in 0.001142 seconds\n\n Computation of PI with 100 places finished in 0.001182 seconds\n\n Computation of PI with 1000 places finished in 0.001206 seconds\n\n Computation of PI with 10000 places finished in 0.001230 seconds\n\n Computation of PI with 100000 places finished in 0.001255 seconds\n\n Computation of PI with 1000000 places finished in 432.574457 seconds\n\n Computation of PI with 1000000 places finished in 402.551226 seconds\n\n DONE\n\nServer\n~~~~~~\n\n.. code:: python\n\n from __future__ import print_function\n\n from collections import defaultdict\n from twisted.internet import defer, reactor, utils\n from twisted.python import failure\n from txmsgpackrpc.server import MsgpackRPCServer\n\n\n pi_chudovsky_bs = '''\n \"\"\"\n Python3 program to calculate Pi using python long integers, binary\n splitting and the Chudnovsky algorithm\n\n See: http://www.craig-wood.com/nick/articles/pi-chudnovsky/ for more\n info\n\n Nick Craig-Wood \n \"\"\"\n\n import math\n from time import time\n\n def sqrt(n, one):\n \"\"\"\n Return the square root of n as a fixed point number with the one\n passed in. It uses a second order Newton-Raphson convgence. This\n doubles the number of significant figures on each iteration.\n \"\"\"\n # Use floating point arithmetic to make an initial guess\n floating_point_precision = 10**16\n n_float = float((n * floating_point_precision) // one) / floating_point_precision\n x = (int(floating_point_precision * math.sqrt(n_float)) * one) // floating_point_precision\n n_one = n * one\n while 1:\n x_old = x\n x = (x + n_one // x) // 2\n if x == x_old:\n break\n return x\n\n def pi_chudnovsky_bs(digits):\n \"\"\"\n Compute int(pi * 10**digits)\n\n This is done using Chudnovsky's series with binary splitting\n \"\"\"\n C = 640320\n C3_OVER_24 = C**3 // 24\n def bs(a, b):\n \"\"\"\n Computes the terms for binary splitting the Chudnovsky infinite series\n\n a(a) = +/- (13591409 + 545140134*a)\n p(a) = (6*a-5)*(2*a-1)*(6*a-1)\n b(a) = 1\n q(a) = a*a*a*C3_OVER_24\n\n returns P(a,b), Q(a,b) and T(a,b)\n \"\"\"\n if b - a == 1:\n # Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)\n if a == 0:\n Pab = Qab = 1\n else:\n Pab = (6*a-5)*(2*a-1)*(6*a-1)\n Qab = a*a*a*C3_OVER_24\n Tab = Pab * (13591409 + 545140134*a) # a(a) * p(a)\n if a & 1:\n Tab = -Tab\n else:\n # Recursively compute P(a,b), Q(a,b) and T(a,b)\n # m is the midpoint of a and b\n m = (a + b) // 2\n # Recursively calculate P(a,m), Q(a,m) and T(a,m)\n Pam, Qam, Tam = bs(a, m)\n # Recursively calculate P(m,b), Q(m,b) and T(m,b)\n Pmb, Qmb, Tmb = bs(m, b)\n # Now combine\n Pab = Pam * Pmb\n Qab = Qam * Qmb\n Tab = Qmb * Tam + Pam * Tmb\n return Pab, Qab, Tab\n # how many terms to compute\n DIGITS_PER_TERM = math.log10(C3_OVER_24/6/2/6)\n N = int(digits/DIGITS_PER_TERM + 1)\n # Calclate P(0,N) and Q(0,N)\n P, Q, T = bs(0, N)\n one = 10**digits\n sqrtC = sqrt(10005*one, one)\n return (Q*426880*sqrtC) // T\n\n if __name__ == \"__main__\":\n import sys\n digits = int(sys.argv[1])\n pi = pi_chudnovsky_bs(digits)\n print(pi)\n '''\n\n\n def set_timeout(deferred, timeout=30):\n def callback(value):\n if not watchdog.called:\n watchdog.cancel()\n return value\n\n deferred.addBoth(callback)\n\n watchdog = reactor.callLater(timeout, defer.timeout, deferred)\n\n\n class ComputePI(MsgpackRPCServer):\n\n def __init__(self):\n self.waiting = defaultdict(list)\n self.results = {}\n\n def remote_PI(self, digits, timeout=None):\n if digits in self.results:\n return defer.succeed(self.results[digits])\n\n d = defer.Deferred()\n\n if digits not in self.waiting:\n subprocessDeferred = self.computePI(digits, timeout)\n\n def callWaiting(res):\n waiting = self.waiting[digits]\n del self.waiting[digits]\n\n if isinstance(res, failure.Failure):\n func = lambda d: d.errback(res)\n else:\n func = lambda d: d.callback(res)\n\n for d in waiting:\n func(d)\n\n subprocessDeferred.addBoth(callWaiting)\n\n self.waiting[digits].append(d)\n\n return d\n\n def computePI(self, digits, timeout):\n d = utils.getProcessOutputAndValue('/usr/bin/python', args=('-c', pi_chudovsky_bs, str(digits)))\n\n def callback((out, err, code)):\n if code == 0:\n pi = int(out)\n self.results[digits] = pi\n return pi\n else:\n return failure.Failure(RuntimeError('Computation failed: ' + err))\n\n if timeout is not None:\n set_timeout(d, timeout)\n\n d.addCallback(callback)\n\n return d\n\n\n def main():\n server = ComputePI()\n reactor.listenTCP(8000, server.getStreamFactory())\n\n if __name__ == '__main__':\n reactor.callWhenRunning(main)\n reactor.run()\n\nClient\n~~~~~~\n\n.. code:: python\n\n from __future__ import print_function\n\n import sys\n import time\n from twisted.internet import defer, reactor, task\n from twisted.python import failure\n\n @defer.inlineCallbacks\n def main():\n try:\n\n from txmsgpackrpc.client import connect\n\n c = yield connect('localhost', 8000, waitTimeout=900)\n\n def callback(res, digits, start_time):\n if isinstance(res, failure.Failure):\n print('Computation of PI with %d places failed: %s' %\n (digits, res.getErrorMessage()), end='\\n\\n')\n else:\n print('Computation of PI with %d places finished in %f seconds' %\n (digits, time.time() - start_time), end='\\n\\n')\n sys.stdout.flush()\n\n defers = []\n for _ in range(2):\n for digits in (5, 100, 1000, 10000, 100000, 1000000):\n d = c.createRequest('PI', digits, 600)\n d.addBoth(callback, digits, time.time())\n defers.append(d)\n # wait for 30 seconds\n yield task.deferLater(reactor, 30, lambda: None)\n\n yield defer.DeferredList(defers)\n\n print('DONE')\n\n except Exception:\n import traceback\n traceback.print_exc()\n finally:\n reactor.stop()\n\n if __name__ == '__main__':\n reactor.callWhenRunning(main)\n reactor.run()\n\nMulticast UDP example\n---------------------\n\nExample servers join to group 224.0.0.5 and listen on port 8000. Their only\nmethod ``echo`` returns its parameter.\n\nClient joins group to 224.0.0.5, sends multicast request to group on port 8000\nand waits for 5 seconds for responses. If some responses are received,\nprotocol callbacks with tuple of results and individual parts are checked for\nerrors. If no responses are received, protocol errbacks with TimeoutError.\n\nBecause there is no common way to determine number of peers in group,\nMsgpackMulticastDatagramProtocol always wait for responses until waitTimeout\nexpires.\n\n.. code:: sh\n\n $ # setup multicast routing\n $ ip route add 224.0.0.0/4 dev eth0\n $ echo 1 > /proc/sys/net/ipv4/ip_forward\n $\n $ # start servers listening on port 8000\n $ python examples/tx_rpc_server_udp_multicast.py &\n [1] 3584\n $ python examples/tx_rpc_server_udp_multicast.py &\n [2] 3585\n $ python examples/tx_rpc_server_udp_multicast.py &\n [3] 3586\n $ python examples/tx_rpc_server_udp_multicast.py &\n [4] 3587\n $ python examples/tx_rpc_server_udp_multicast.py &\n [5] 3588\n $\n $ # execute client\n $ python examples/tx_rpc_client_udp_multicast.py\n Received results from 5 peers\n $\n\nServer\n~~~~~~\n\n.. code:: python\n\n from twisted.internet import defer, reactor, task\n from txmsgpackrpc.server import MsgpackRPCServer\n\n\n class EchoRPC(MsgpackRPCServer):\n\n @defer.inlineCallbacks\n def remote_echo(self, value, delay=None, msgid=None):\n if delay is not None:\n yield task.deferLater(reactor, delay, lambda: None)\n defer.returnValue(value)\n\n\n def main():\n server = EchoRPC()\n reactor.listenMulticast(8000, server.getMulticastProtocol('228.0.0.5', ttl=5),\n listenMultiple=True)\n\n if __name__ == '__main__':\n reactor.callWhenRunning(main)\n reactor.run()\n\n\nClient\n~~~~~~\n\n.. code:: python\n\n from __future__ import print_function\n\n from twisted.internet import defer, reactor\n\n @defer.inlineCallbacks\n def main():\n try:\n\n from txmsgpackrpc.client import connect_multicast\n\n c = yield connect_multicast('228.0.0.5', 8000, ttl=5, waitTimeout=5)\n\n data = {\n 'firstName': 'John',\n 'lastName': 'Smith',\n 'isAlive': True,\n 'age': 25,\n 'height_cm': 167.6,\n 'address': {\n 'streetAddress': \"21 2nd Street\",\n \"city\": 'New York',\n \"state\": 'NY',\n 'postalCode': '10021-3100'\n },\n 'phoneNumbers': [\n {\n 'type': 'home',\n 'number': '212 555-1234'\n },\n {\n 'type': 'office',\n 'number': '646 555-4567'\n }\n ],\n 'children': [],\n 'spouse': None\n }\n\n results = yield c.createRequest('echo', data)\n\n assert isinstance(results, tuple)\n\n print('Received results from %d peers' % len(results))\n\n for i, result in enumerate(results):\n if result != data:\n print('Result %d mismatch' % i)\n print(result)\n\n except Exception:\n import traceback\n traceback.print_exc()\n finally:\n reactor.stop()\n\n if __name__ == '__main__':\n reactor.callWhenRunning(main)\n reactor.run()\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/jakm/txmsgpackrpc", "keywords": "twisted msgpack rpc", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "txmsgpackrpc", "package_url": "https://pypi.org/project/txmsgpackrpc/", "platform": "", "project_url": "https://pypi.org/project/txmsgpackrpc/", "project_urls": { "Homepage": "https://github.com/jakm/txmsgpackrpc" }, "release_url": "https://pypi.org/project/txmsgpackrpc/1.2/", "requires_dist": null, "requires_python": "", "summary": "txmsgpackrpc is a Twisted library to support msgpack-rpc", "version": "1.2" }, "last_serial": 2526528, "releases": { "0.4": [ { "comment_text": "", "digests": { "md5": "2800a7c440bf8bcbb2dc6f2552493a9d", "sha256": "0cc0be7e6b4f72207d0c20d1378d266be33133eabba88d9c4324fcdbf2da89c9" }, "downloads": -1, "filename": "txmsgpackrpc-0.4.tar.gz", "has_sig": false, "md5_digest": "2800a7c440bf8bcbb2dc6f2552493a9d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11611, "upload_time": "2015-02-20T09:59:23", "url": "https://files.pythonhosted.org/packages/93/44/930b1a5d43489bdb882da509d47dbae4e47a1eb14271fd373ff5d1b30567/txmsgpackrpc-0.4.tar.gz" } ], "0.5": [ { "comment_text": "", "digests": { "md5": "b93eaf6d303e1e8e611c92638c8e8929", "sha256": "83c3c52a49bc4d19b2fc17e16873ca8cfc8c0919c4c238aebc03850d28aee5e8" }, "downloads": -1, "filename": "txmsgpackrpc-0.5.tar.gz", "has_sig": false, "md5_digest": "b93eaf6d303e1e8e611c92638c8e8929", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14693, "upload_time": "2015-02-24T23:20:50", "url": "https://files.pythonhosted.org/packages/4f/df/0cd8de3aeec403c4b82040e07bb685f660e24e6e482ff99c5e2df555bca3/txmsgpackrpc-0.5.tar.gz" } ], "1.0": [ { "comment_text": "", "digests": { "md5": "e1825be3865cde1e013f953114a5cfd0", "sha256": "5e52a25f07ccd3b10558ef5462031142cc4dfe404a2f3431542d96c409280fae" }, "downloads": -1, "filename": "txmsgpackrpc-1.0.tar.gz", "has_sig": false, "md5_digest": "e1825be3865cde1e013f953114a5cfd0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 18944, "upload_time": "2015-02-25T17:27:24", "url": "https://files.pythonhosted.org/packages/c8/e2/a132bc15f88114de20aeb59aac6e0497b1d09c1d575f2f1ab1f3c4decd29/txmsgpackrpc-1.0.tar.gz" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "563c23246af76d4dbfeb9037b5f0c06e", "sha256": "e3ad34850f450b86be6f1a85a3c3d6d31e61ce797d97d681431ad29d7d6d83ce" }, "downloads": -1, "filename": "txmsgpackrpc-1.1.tar.gz", "has_sig": false, "md5_digest": "563c23246af76d4dbfeb9037b5f0c06e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 18560, "upload_time": "2015-10-06T09:14:02", "url": "https://files.pythonhosted.org/packages/69/81/25ac3feea9af76f684e6d1a2dbf4d1e1f20ed612b7f9236e96a3a4499e4e/txmsgpackrpc-1.1.tar.gz" } ], "1.2": [ { "comment_text": "", "digests": { "md5": "3001608f140b5aed2680ecad113e1f84", "sha256": "8740361ae55781bc7ee1f639fa398fd0c6f892d0046879716cd6ab3091c41ac3" }, "downloads": -1, "filename": "txmsgpackrpc-1.2.tar.gz", "has_sig": false, "md5_digest": "3001608f140b5aed2680ecad113e1f84", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 21020, "upload_time": "2016-12-18T13:39:10", "url": "https://files.pythonhosted.org/packages/6e/58/fefe6b5206b9a7d5f13a55917e132c674e50f4be8c167ff1dbba3c983f02/txmsgpackrpc-1.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "3001608f140b5aed2680ecad113e1f84", "sha256": "8740361ae55781bc7ee1f639fa398fd0c6f892d0046879716cd6ab3091c41ac3" }, "downloads": -1, "filename": "txmsgpackrpc-1.2.tar.gz", "has_sig": false, "md5_digest": "3001608f140b5aed2680ecad113e1f84", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 21020, "upload_time": "2016-12-18T13:39:10", "url": "https://files.pythonhosted.org/packages/6e/58/fefe6b5206b9a7d5f13a55917e132c674e50f4be8c167ff1dbba3c983f02/txmsgpackrpc-1.2.tar.gz" } ] }