Oop 基于Twisted的Python面向对象设计

Oop 基于Twisted的Python面向对象设计,oop,twisted,Oop,Twisted,我的问题是关于如何设计能与面向对象设计和异步延迟(而不是块代码)一起工作的代码 好的,我在考虑设计课程的两种方式(这些设计是好的还是我忘了什么) 第一条路 class Grooveshark(object): def get_session(self): d = # implementation detail (gets page) d.addCallback(self.parse_session)# implmentation detail # all in all t

我的问题是关于如何设计能与面向对象设计和异步延迟(而不是块代码)一起工作的代码

好的,我在考虑设计课程的两种方式(这些设计是好的还是我忘了什么)

第一条路

class Grooveshark(object):
def get_session(self):
    d = # implementation detail (gets page)
    d.addCallback(self.parse_session)# implmentation detail
    # all in all this goes through and sets self.session to the session value (it does not return it though; should I set it and return it?)
    self.session_time = time.time()
    return d
def get_country_id(self):
    # implmentation acts same as d just parses to diferrent id
    # also it grabs the same page; ANNOYING having to get the page twice ugh

def get_token(self):
    # relies on self.session being set
    d = # implmentation detail
    d.addCallback(self.parse_token)# implmentation detail
    # at the end of the day it sets self.token and then fires the deferred (same as session does not send it through the deferred, again should I seem repetitive?)
    return d
def construct_api_call(method, url, implmentation_arguments...)
    # checks if session is hour old
    if self.session_time - 3600 <= time.time() or self.session is None:

        # update
        d = get_session()
        # some how pass deferred or something
        d.addCallback(lambda ignored: self.get_country_id)
        d.addCallback(lambda ignored: self.get_token())
        d.addCallback(lambda ignored: self.construct_api_call(method, url, implmentation_arguments)
        # if error retry
        d.addErrback(lambda error: log.err(error))
        d.addErrback(lambda ignored: self.construct_api_call(method, url, implmentation_arguments)
        return d# would this work?  problem: with this how do I get it so I can do this whole update and do this again with the deferred I returned

    else:
        #implmentation details
        return d# fires when done with api call
类Grooveshark(对象):
def get_会话(自):
d=#实现细节(获取页面)
d、 addCallback(self.parse_session)#实现细节
#总而言之,这将完成并将self.session设置为session值(但它不会返回它;我应该设置它并返回它吗?)
self.session\u time=time.time()
返回d
def获取国家/地区id(自我):
#实现的作用与d只是解析为不同的id相同
#它还抓住了同一页;两次翻页真烦人
def get_令牌(自身):
#依赖于正在设置的self.session
d=#实施细节
d、 addCallback(self.parse_token)#实现细节
#在一天结束时,它设置self.token,然后触发延迟(与会话不通过延迟发送相同,我应该重复吗?)
返回d
def构造api调用(方法、url、实现参数…)
#检查会话是否已开始一小时
如果self.session_time-3600简短回答:参见

无论是函数式编程还是面向对象编程,使用Twisted的关键和好处在于它使用回调事件驱动设计来允许异步程序执行。一个常见的观察结果是,事件驱动编程需要改变编码风格和布局——正如您的问题所示

在几乎所有情况下,在方法或函数中使用“@defer.inlineCallbacks”修饰符将有助于使扭曲的代码模块化并可重用。当您使用这种方法编写源代码时,您可以编写异步代码,而不是在这么多函数之间“拆分”。每次代码块需要转到下一个阻塞或“延迟”段时,它都使用yield命令。这允许函数在延迟完成时继续其停止的位置。这使得回调链看起来像常规的阻塞代码


@Zimmer:我不太确定你在问什么。您能否更正代码的格式,包括可执行代码和/或描述您的设计困境?当代码的目的不明确时,我发现很难考虑设计。
class Grooveshark(object):
def get_session(self):

    d = # implmentation detail
    # differance this sends the session down the deferred callback and sets the instance var self.session (seems strange both modifying state and returning)

def get_token(self, session):
    d = # gets token but uses session argument NOT intance variable

def get_country_id # same as first class 

def construct_api_call(session, session_time, token, country_id, all the other args in above class):
    # problems it requires user of api to store session etc also how do I return if needs update right now I just error
    if self.session_time - 3600 <= time.time():
        raise RuntimeError("You need to update your session, ugh why does the user have to store the session ugh")

    else:
        # does what above class does as well