Skip to main content

A persistent process pool for Twisted

Project description

Summary

A Python persistent process pool for use with Twisted. Provides the ability to run Python callables asynchronously within a pool of persistent processes, as long as the callable, its arguments, and its return value are all picklable.

Installing

pip install txpool

or

python setup.py install

Examples

Here are some simple examples to give you the idea:

import glob
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
import txpool

pool = txpool.Pool()

@inlineCallbacks
def main():
    result = yield pool.apply_async(glob.glob, ('*.pdf',))
    print result
    reactor.stop()

reactor.callWhenRunning(main)
reactor.run()

The callable can instead be specified as a string, using dotted notation to specify the full path to the callable.

from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
import txpool

pool = txpool.Pool()

@inlineCallbacks
def main():
    # You can provide an optional timeout (in seconds) for the call
    # (the default is None).
    try:
        result = yield pool.apply_async('glob.glob', ('*.pdf',), timeout=5)
    except PoolTimeout as e:
        result = e
    print result
    reactor.stop()

reactor.callWhenRunning(main)
reactor.run()

The txpool.Pool class can be explicitly sized, asked to log its actions, and/or given a custom name.

import logging
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks, gatherResults
import txpool

logger = logging.getLogger('example')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

pool = txpool.Pool(size=5, log=logger, name='twisting-by-the-pool')

@inlineCallbacks
def main():
    calls = ('math.factorial',) * 5
    args = [(n,) for n in range(150780, 150785)]

    # You can wait until the pool is at full-strength (providing an
    # optional timeout if desired), but it's not required before
    # calling the "apply_async" method.  Jobs are queued until a
    # worker process is available.
    try:
        yield pool.on_ready(timeout=10)
    except PoolTimeout as e:
        results = e
    else:
        results = yield gatherResults(map(pool.apply_async, calls, args))

    print results

    try:
        # You can gracefully close the pool, which ensures all jobs
        # already queued are completed before shutting down...
        yield pool.close(timeout=10)
    except PoolTimeout as e:
        print e
        # ...or you can use force and immediately send SIGKILL to each
        # process in the pool.
        yield pool.terminate(timeout=10)

    reactor.stop()

reactor.callWhenRunning(main)
reactor.run()

Project details


Release history Release notifications | RSS feed

This version

0.9

Download files

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

Source Distribution

txpool-0.9.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

txpool-0.9-py2-none-any.whl (11.7 kB view details)

Uploaded Python 2

File details

Details for the file txpool-0.9.tar.gz.

File metadata

  • Download URL: txpool-0.9.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for txpool-0.9.tar.gz
Algorithm Hash digest
SHA256 e2847c67c10f1f44d7a2394afac653fa772564d305c36c3c089f1ea1ff85f3db
MD5 f61ffe48335e698d825c15345f350899
BLAKE2b-256 57e5a4f40f6c6d8dc8b849cdca45cc4fba9c23b0417ada37b20835ed436fbc08

See more details on using hashes here.

File details

Details for the file txpool-0.9-py2-none-any.whl.

File metadata

  • Download URL: txpool-0.9-py2-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for txpool-0.9-py2-none-any.whl
Algorithm Hash digest
SHA256 5377461b57f6d0db0245cb65dfa68fa22dfd9e704328e1c2462cdf02e67e5da2
MD5 8123da45af40ed2dfef3c2049650ea0d
BLAKE2b-256 5c870e9608062ce97bd97fb0bf8ebb0a8683ab4418f5e049fbd870f0f2a1555b

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