简体   繁体   English

getPage内的延迟对象打印?

[英]getPage inside of deferred object printing?

I'd like to create a callback that does a getPage on a specific url and prints that out when the operation is complete. 我想创建一个对特定网址执行getPage的回调,并在操作完成后将其打印出来。 Currently when I print d or page (see code below), I get a reference to the deferred object vs. the contents of page . 当前,当我打印dpage (请参见下面的代码),我得到了对延迟对象和page内容的引用。

Why does the memory location of the deferred object change between print page and print d ? 为什么延迟对象的存储位置在打印page和打印d之间改变?

Eventually I'd like this program to cycle through a list of 4 of my websites, create callbacks for each of those individual connections, fire them off, and print each page as they are ready. 最终,我希望该程序循环浏览我的4个网站,为每个单独的连接创建回调,将其触发,并在准备好页面时打印每个页面。 If it's not too much to ask, can this be demonstrated? 如果不是要问太多,可以证明这一点吗?

from twisted.web.client import getPage
from twisted.internet import reactor
from twisted.internet.defer import Deferred

def connect(url):
    page = getPage(url)

print page returns <Deferred object at 0x23dcc68> . print page返回<Deferred object at 0x23dcc68>

print d returns <Deferred object at 0x7f1bacacc3b0> . print d返回<Deferred object at 0x7f1bacacc3b0>

Current result (using 'http://www.example.com' as an example): 当前结果(以“ http://www.example.com”为例):

d = Deferred()

d.addCallback(connect)

reactor.callWhenRunning(d.callback, 'http://www.example.com')

reactor.callLater(4, reactor.stop)

reactor.run()

You should probably be using the newer, spiffier twisted.web.client.Agent rather than the older and somewhat limited getPage . 您应该使用的是较新的,更复杂的twisted.web.client.Agent而不是较旧的且受限制的getPage Lucky for you there is a very thorough tutorial on how to use Agent , as well as some of its companion classes like ProxyAgent , RedirectAgent , CookieAgent , and ContentDecoderAgent . 幸运的是,您有一个关于如何使用Agent的非常详尽的教程 ,以及一些诸如ProxyAgentRedirectAgentCookieAgentContentDecoderAgent伴随类。

First, though, you may want to familiarize yourself with the documentation on how to use Deferred s . 但是,首先,您可能想熟悉有关如何使用Deferred文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM