日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

ASPN : Python Cookbook : [Twisted] From block...

 weicat 2007-08-03

## version 1.1, changed according to the suggestions in the comments
            from twisted.internet import reactor, defer
            from twisted.python import threadable; threadable.init(1)
            import sys, time
            ## the wrong way
            def callInThread(func, *args):
            """Takes a blocking function an converts it into a deferred-valued
            function running in a separate thread.
            """
            de = defer.Deferred()
            de.addCallback(func)
            reactor.callInThread(de.callback, *args)
            return de
            deferred = callInThread.__get__ # decorator associated to callInThread
            # the right way
            from twisted.internet.threads import deferToThread
            deferred = deferToThread.__get__
            ## example code
            def print_(result):
            print result
            def running():
            "Prints a few dots on stdout while the reactor is running."
            sys.stdout.write("."); sys.stdout.flush()
            reactor.callLater(.1, running)
            @deferred
            def sleep(sec):
            "A blocking function magically converted in a non-blocking one."
            print ‘start sleep %s‘ % sec
            time.sleep(sec)
            print ‘\nend sleep %s‘ % sec
            return "ok"
            if __name__ == "__main__":
            sleep(2).addBoth(print_)
            reactor.callLater(.1, running)
            reactor.callLater(3, reactor.stop)
            reactor.run()

Discussion:

How to make blocking code non-blocking is the obvious question for
everybody using Twisted, but the Twisted documentation does not make
easy to find the solution :-(

The trick is to run the blocking function in a separate thread. Here
all the magic is performed by the decorator, ``deferred``, which converts
``sleep``, a blocking function, into a deferred function i.e. a
non-blocking function that returns a deferred object. The code for
``callInThread`` is clear, and the ``.__get__`` trick converts
``callInThread`` in a one-argument function returning a closure,
i.e. an object suitable to be used as a decorator. I have seen this
trick in Alex Martelli‘s lectures at PyCon 2005. If you are confused
by it, you should read Raymond Hettinger essay on descriptors
(http://users./python/download/Descriptor.htm).

In short: every time you have a blocking function in your code, wrap
it with the ``deferred`` decorator and live happy!




    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多