Python扭曲:如何安排? [英] Python twisted: how to schedule?

查看:54
本文介绍了Python扭曲:如何安排?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Twisted 中有 1 天的经验,我尝试安排消息发送以回复 tcp 客户端:

Having 1-day experience in Twisted I try to schedule message sending in reply to tcp client:

import os, sys, time
from twisted.internet import protocol, reactor

self.scenario = [(1, "Message after 1 sec!"), (4, "This after 4 secs"), (2, "End final after 2 secs")]
for timeout, data in self.scenario:
        reactor.callLater(timeout, self.sendata, data)
        print "waited %d time, sent %s\n"%(timeout, data)

现在它发送消息,但我有两个问题:
1)超时"从现在"开始,我想在每个上一个任务完成后计算它(上一个消息已发送)
2)我不知道如何在发送完所有消息后关闭连接.如果我将 self.transport.loseConnection() 放在 callLater 之后,它会立即关闭连接.

Now it sends messages, but I have 2 problems:
1) "timeout" is going from "now", and I want to count it after each previous task was completed (previous message was sent)
2) I don't know how to close connection after all messages were sent. If I place self.transport.loseConnection() after callLaters it closes connection immediately.

在之前的尝试中,我没有使用 reactor.callLater,而是只使用了 self.transport.write()time.sleep(n)for 循环中的代码>.在这种情况下,所有消息都在所有超时过后一起发送......不是我想要的.
目的是等待客户端连接,等待timeout1并发送message1,等待timeout2并发送message2,......等最后一条消息后-关闭连接.

In previous try I didn't use reactor.callLater, but only self.transport.write() and time.sleep(n) in for loop. In this case all messages were sent together after all timeouts passed... Not something I wanted.
The purpose is to wait for client connection, wait timeout1 and send message1, wait timeout2 and send message2, ... etc. After final message - close connection.

推荐答案

在使用 Twisted 时要认识到的重要一点是没有任何事情等待任何事情.当您调用 reactor.callLater() 时,您是在要求 reactor 稍后调用某些东西,而不是现在.调用立即结束(在调用被调度之后,之前它被执行.)因此,你的 print 语句是一个谎言:你没有等待 超时时间;你根本没有等.

The important thing to realize when working with Twisted is that nothing waits for anything. When you call reactor.callLater(), you're asking the reactor to call something later, not now. The call finishes right away (after the call has been scheduled, before it has been executed.) Consequently, your print statement is a lie: you didn't wait timeout time; you didn't wait at all.

您可以通过多种方式修复它,使用哪种方式取决于您的实际需求.如果您希望第二个任务在第一个任务开始后四秒开始,您可以简单地将第一个任务的延迟(您的 timeout 变量)添加到第二个任务.但是,第一个任务可能不会在您安排它时准确开始;如果 Twisted 太忙而不能早点开始,它可能会晚一点开始.此外,如果您的任务需要很长时间,在第二个任务开始之前可能实际上无法完成.

You can fix it in multiple ways, and which to use depends on what you actually want. If you want the second task to start four seconds after the first task started, you can simply add the delay (your timeout variable) of the first task to the delay of the second task. The first task may not start exactly when you schedule it, though; it may start later, if Twisted is too busy to start it sooner. Also, if your task takes a long time it may not actually be done before the second task starts.

更常见的方式是让第一个任务调度第二个任务,而不是马上调度第二个任务.您可以在第一个任务结束后 4 秒(通过在第一个任务结束时调用 reactor.callLater())或在第一个任务开始后 4 秒(通过调用 reactor.callLater() 在第一个任务的开始),或者执行更复杂的计算来确定它应该何时开始,跟踪经过的时间.

The more common way is for the first task to schedule the second task, instead of scheduling the second task right away. You can schedule it four seconds after the first task ended (by calling reactor.callLater() at the end of the first task), or four seconds after the first task started (by calling reactor.callLater() at the start of the first task), or perform more complex calculations to determine when it should start, keeping track of elapsed time.

当您在 Twisted 等待中什么都没有意识到时,在您执行完所有计划任务后处理关闭连接变得很容易:您只需调用最后一个任务 self.transport.loseConnection().对于更复杂的情况,您可能希望将 Deferred 链接在一起,或使用 DeferredList 在所有待处理任务完成后执行 loseConnection(),即使它们不是严格顺序的.

When you realize nothing in Twisted waits, dealing with closing the connection when you've performed all scheduled tasks becomes easy: you simply have your last task call self.transport.loseConnection(). For more complex situations you may want to chain Deferreds together, or use a DeferredList to perform the loseConnection() when all pending tasks have finished, even when they aren't strictly sequential.

这篇关于Python扭曲:如何安排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆