Multithreading Twisted Reactor不可重启-重复使用异步线程

Multithreading Twisted Reactor不可重启-重复使用异步线程,multithreading,twisted,reactor,Multithreading,Twisted,Reactor,我有一个URL列表。我希望每10秒异步获取它们的内容 urls = [ 'http://www.python.org', 'http://stackoverflow.com', 'http://www.twistedmatrix.com', 'http://www.google.com', 'http://launchpad.net', 'http://github.com', 'http://bitbucket.org', ] waiting = [client.getP

我有一个URL列表。我希望每10秒异步获取它们的内容

urls = [
 'http://www.python.org', 
 'http://stackoverflow.com', 
 'http://www.twistedmatrix.com', 
 'http://www.google.com',
 'http://launchpad.net',
 'http://github.com',
 'http://bitbucket.org',
]

waiting = [client.getPage(url) for url in urls]
defer.gatherResults(waiting).addCallback(saveResults)

reactor.run()
我该怎么做?这段代码只允许我获取一次URL内容。再次调用它会抛出
错误。ReactorNotRestartable()


谢谢:)

这在Twisted上绝对是可能的

首先,尽管这与您的问题有些无关,但不要使用
getPage
。这是一个非常有限的API,HTTPS上的安全默认值很差。相反,使用

现在,谈谈你的主要问题

关于
reactor.run()
需要了解的重要一点是,它并不意味着“在这里运行此代码”。它的意思是“运行整个程序”。当
reactor.run()
退出时,是您的程序退出的时候了

幸运的是,Twisted有一种很好的内置方式,可以按常规计划做事:

下面是一个工作示例,使用
treq
LoopingCall

urls = [
    'http://www.python.org',
    'http://stackoverflow.com',
    'http://www.twistedmatrix.com',
    'http://www.google.com',
    'http://launchpad.net',
    'http://github.com',
    'http://bitbucket.org',
]

from twisted.internet.task import LoopingCall
from twisted.internet.defer import gatherResults
from treq import get, content

def fetchWebPages():
    return (gatherResults([get(url).addCallback(content) for url in urls])
            .addCallback(saveResults))

def saveResults(responses):
    print("total: {} bytes"
          .format(sum(len(response) for response in responses)))


repeatedly = LoopingCall(fetchWebPages)
repeatedly.start(10.0)

from twisted.internet import reactor
reactor.run()

作为奖励,这可以处理
fetchWebPages
所需时间超过10秒的情况,并将智能地做出反应,而不是让太多未完成的请求堆积起来,或者随着请求时间的延长而延迟越来越长。

本例中没有线程。你打算包括一些吗?