【问题标题】:How do I queue FTP commands in Twisted?如何在 Twisted 中排队 FTP 命令?
【发布时间】:2010-11-09 00:04:40
【问题描述】:

我正在使用 Twisted 编写一个 FTP 客户端,它可以下载大量文件,并且我正在尝试非常聪明地完成它。但是,我一直遇到的问题是我会非常快速地下载几个文件(有时每批约 20 个,有时约 250 个)然后下载会挂起,只是最终连接超时,然后下载并挂起开始重新来过。我使用 DeferredSemaphore 一次只能下载 3 个文件,但我现在怀疑这可能不是避免限制服务器的正确方法。

这里是有问题的代码:

def downloadFiles(self, result, directory):
    # make download directory if it doesn't already exist
    if not os.path.exists(directory['filename']):
        os.makedirs(directory['filename'])

    log.msg("Downloading files in %r..." % directory['filename'])

    files = filterFiles(None, self.fileListProtocol)
    # from http://stackoverflow.com/questions/2861858/queue-remote-calls-to-a-python-twisted-perspective-broker/2862440#2862440
    # use a DeferredSemaphore to limit the number of files downloaded simultaneously from the directory to 3
    sem = DeferredSemaphore(3)
    jobs = [sem.run(self.downloadFile, f, directory) for f in files]
    d = gatherResults(jobs)
    return d

def downloadFile(self, f, directory):
    filename = os.path.join(directory['filename'], f['filename']).encode('ascii')
    log.msg('Downloading %r...' % filename)
    d = self.ftpClient.retrieveFile(filename, FTPFile(filename))
    return d

您会注意到我正在重用 FTP 连接(顺便说一下,是活动的)并使用我自己的 FTPFile 实例来确保在文件下载连接“丢失”(即完成)时关闭本地文件对象.看着FTPClient,我想知道我是否应该直接使用queueCommand。老实说,我在retrieveFile 命令到_openDataConnection 之后迷路了,所以也许它已经被使用了。

有什么建议吗?谢谢!

【问题讨论】:

    标签: python ftp twisted ftp-client


    【解决方案1】:

    我建议使用 queueCommand,正如您所建议的那样,我怀疑您使用的信号量可能会导致您出现问题。我相信使用 queueCommand 会将您的 FTPClient 限制为单个活动连接(尽管我只是在推测),因此如果您想快速完成操作,您可能需要考虑创建一些 FTPClient 实例并将下载作业传递给它们。如果您使用queueStringCommand,您会得到一个 Deferred,您可以使用它来确定每个客户端的进度,甚至在回调中为该客户端添加另一个作业到队列中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-30
      • 2016-01-30
      • 1970-01-01
      • 2021-05-06
      • 2015-07-25
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      相关资源
      最近更新 更多