Python论坛  - 讨论区

标题:Re[4]: _[python-chinese]_一个生成器版本的ussc.py

2004年08月02日 星期一 09:12

Zoom.Quiet zoomq at infopro.cn
Mon Aug 2 09:12:31 HKT 2004

Hollo info:

  可能是翻译的问题!
还是看Limodou 的介绍吧!
http://www.donews.net/limodou/archive/2004/07/10/40913.aspx


/******** [2004-08-02]09:12:09 ; info wrote:

info at xichen.com> hoxide,您好!

info at xichen.com> http://www-900.ibm.com/developerWorks/cn/linux/sdk/python/charm-20/index.shtml

info at xichen.com> 	没看懂,能讲解一下吗?

info at xichen.com> ======= 2004-08-01 12:19:47 您在来信中写道:=======

>>HD,您好!
>>
>>大家最好先了解一下生成器的有关知识:IBM上的《可爱的
>>Python:迭代器和简单生成器》:
>>http://www-900.ibm.com/developerWorks/cn/linux/sdk/python/charm-20/index.shtml
>>
>>昨天给的代码还有点错误,修改的代码(其实只改了打星号的地方):
>>
>>    def testserver(self):
>>        """向服务器发的测试报文"""
>>        try:
>>            self.pp.next()
>>            self.call = reactor.callLater(0,self.testserver)
>>            return
>>        except StopIteration:
>>            pass
>>   
>>    def __init__(self):
>>        self.pp=self.sendQQ()
>>        ussp.USSClientQueueProtocol.__init__(self)
>>        
>>    def sendQQ(self):
>>        global nownum
>>        global count
>>        i=1
>>        while i < count:
>>            message = usspmsg.USSPMessage()
>>            message.setMsgName('mail_counter')
>>            message.body.setField('uid',str(i))  
>>            while self.factory.sendQueue.full():        #*  
>>                yield None                              #**
>>            self.factory.sendQueue.put_nowait(message)
>>            i += 1
>>        nownum=i
>>        self.disconnect()
>>
>>这里真正的处理是在sendQQ这个函数定义的.self.pp是生成器的实例,由__init__()生成,testserver只是调度完成这个处理的函数,而且是和具体的处理独立的,他只是简单得实现了当处理"暂停"后的重新启动.
>>
>>真正神奇的地方是打星号的行.他测试sendQueue确定是否能发出message,如果不能就会执行yield
>>None,这时函数就终止在**这行,直到在有.next()方法调用时再从这句开始执行.
>>这个好处是原来的处理流程可以很顺利得进行.不需要保存中间变量.注意0731a的testserver能正确得发出所有message,是因为恰巧有全局变量nownum完全确定处理的执行状态了.但事事上一般的处理不会那么简单,有复杂的状态组合(上面的代码并没用nownum,而是用了循环变量i,注意他不是全局的!!!!!).正像zoomq昨天说的0731a的testserver在queue满的时候前面对message的处理就被抛弃了,但在这个生成器版本,先前对message的处理没有被抛弃.
>>
>>个人觉得这个生成器版本还是不够完美,异步传输还是应该以线程为基础进行.下一个版本可能是基于生成器的简单线程:)
>>
>>说得不是很明白,我不清楚大家对什么地方有疑问.
>>
>>
>>===== 2004-08-01 00:33:08 您在来信中写道:=======
>>
>>>细说说,为什么说是生成器版的呢?
>>>
>>>On Sat, 31 Jul 2004 23:22:53 +0800, hoxide
>>><hoxide_dirac at yahoo.com.cn> wrote:
>>>> python-chinese,您好!
>>>> 
>>>>          
>>>> 为什么要用生成器,现在的testserver的执行流程只依赖于nownum,而事实上通常的服务要依赖于整个运行流程.另外这样的写法可将窗口部分的代码抽出.
>>>> 
>>>>>>>> 礼!
>>>> 
>>>> hoxide
>>>> hoxide_dirac at yahoo.com.cn
>>>> 2004-07-31
>>>> 
>>>> 
>>>> 
>>>
>>>
>>>-- 
>>>HD(燃烧中的火)
>>>我工作我快乐,我勤奋我收获。请与我一起快乐,与我一起收获。
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>>
>>
>>= = = = = = = = = = = = = = = = = = = =
>>			
>>
>>        致
>>礼!
>> 
>>				 
>>        hoxide
>>        hoxide_dirac at yahoo.com.cn
>>          2004-08-01
>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>

info at xichen.com> = = = = = = = = = = = = = = = = = = = =
			

info at xichen.com>         致
info at xichen.com> 礼!
 
				 
info at xichen.com>         info
info at xichen.com>         info at xichen.com
info at xichen.com>           2004-08-02



********************************************/

-- 
Free as in Freedom

 Zoom.Quiet                           

#=========================================#
]Time is unimportant, only life important![
#=========================================#

sender is the Bat!2.12.00



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号