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