zoukankan      html  css  js  c++  java
  • twsited使用延迟项,并发量限制

    # -*- coding: utf-8 -*-
    # @Time : 2020/12/16 4:28 下午
    # @Author : chenxiangan
    # @File : demo.py
    # @Software: PyCharm
    
    # d = defer.Deferred()
    # print d.called #延迟项本质代表一个值
    # d.callback(3) #d调用callback之后值变为true,然后可以在延迟链上添加新的项
    # print d.called
    # print d.result
    # d.callback(4)
    # print d.called
    
    # def foo(v):
    #     print "foo called"
    #     return v+1
    #
    # d = defer.Deferred()
    # d.addCallback(foo) #设置回调函数
    # print d.called
    # d.callback(3) #传入3然后调用foo函数
    # print d.called
    # 定义两个延迟项
    # a = defer.Deferred()
    # b = defer.Deferred()
    # def status(*ds):
    #     """
    #     该函数打印延迟项的状态 ,通过result属性可以看到结果
    #     """
    #     return [(getattr(d,'result',"N/A"),len(d.callbacks)) for d in ds]
    #
    # def b_callback(arg):
    #     print "b_call called with arg =",arg
    #     # 返回延迟项b
    #     return b
    #
    # def on_done(arg):
    #     print "on_done called with arg =",arg
    #     return arg
    #
    # #通过addCallback设置回调,然后返回的是b,再给b设置回调为函数on_done。
    # print a.addCallback(b_callback).addCallback(on_done)
    #
    # print status(a,b) #此时两个延迟项都没有被触发
    # b.callback(4)
    # print status(a,b)
    # a.callback(3)
    # print  status(a,b)
    
    
    from twisted.internet import defer
    from twisted.internet import reactor
    from twisted.internet import task
    
    
    # def schedule_install(customer):
    #     def schedule_install_wordpress():
    #         def on_done():
    #             print "Callback: Finished installation for", customer
    #         print "Scheduling: Installation for", customer
    #         return task.deferLater(reactor, 3, on_done)
    #     def all_done(_):
    #         print "All done for", customer
    #
    #     d = schedule_install_wordpress()
    #     d.addCallback(all_done)
    #     return d
    #
    # def twisted_developer_day(customers):
    #     print "Goodmorning from Twisted developer"
    #     work = [schedule_install(customer) for customer in customers]
    #     join = defer.DeferredList(work)
    #     join.addCallback(lambda _: reactor.stop())
    #     print "Bye from Twisted developer!"
    # twisted_developer_day(["Customer %d" % i for i in xrange(15)])
    # reactor.run()
    
    # 简写之后
    # @defer.inlineCallbacks
    # def inline_install(customer):
    #     print "Scheduling: Installation for", customer
    #     yield task.deferLater(reactor, 3, lambda: None) #暂停当前的inline_install(),被触发时再继续。
    #     print "Callback: Finished installation for", customer
    #     print "All done for", customer
    # def twisted_developer_day(customers):
    #     print "Goodmorning from Twisted developer"
    #     work = [inline_install(customer) for customer in customers]
    #     join = defer.DeferredList(work)
    #     join.addCallback(lambda _: reactor.stop())
    #     print "Bye from Twisted developer!"
    #
    # twisted_developer_day(["Customer %d" % i for i in xrange(15)])
    # reactor.run()
    
    # 如果顾客数增加到10000
    @defer.inlineCallbacks
    def inline_install(customer):
        print "Scheduling: Installation for", customer
        yield task.deferLater(reactor, 3, lambda: None)  # 暂停当前的inline_install(),被触发时再继续。
        print "Callback: Finished installation for", customer
        print "All done for", customer
    
    
    def twisted_developer_day(customers):
        print "Goodmorning from Twisted developer"
        work = (inline_install(customer) for customer in customers)
        coop = task.Cooperator()
        # 每次只能并发5个
        join = defer.DeferredList([coop.coiterate(work) for i in xrange(5)])
        join.addCallback(lambda _: reactor.stop())
        print "Bye from Twisted developer!"
    
    
    twisted_developer_day(["Customer %d" % i for i in xrange(15)])
    reactor.run()
    
    

    from:Learning Scrapy

  • 相关阅读:
    greenplum日常维护手册
    Android UI界面基本属性 大全
    Listview 选项按下去黑了所有按钮的解决方法 ——android:cacheColorHint=“#00000000”
    【转】Android应用程序模块详解
    android退出有多个activity的应用
    启动模式"singleTask"和FLAG_ACTIVITY_NEW_TASK具有不同的行为!
    Android 按两次back键退出 效率最高版
    【转】跑马灯效果
    Sundy笔记__Git版本控制
    如果你想用对话框代替一个activity的话,可以设置activity的主题属性
  • 原文地址:https://www.cnblogs.com/c-x-a/p/14145334.html
Copyright © 2011-2022 走看看