Giter Site home page Giter Site logo

txpool's Introduction

txpool

Build Status

Summary

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

Installing

pip install txpool

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

txpool's People

Contributors

escattone avatar rjohnson-twist avatar omit66 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.