免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3793 | 回复: 6
打印 上一主题 下一主题

asyncore回调的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-30 11:22 |只看该作者 |倒序浏览
模仿例子写了一个测试程序,发现连接服务器成功后没有回调handle_connect,为什么?

--------------------------------
class itsocket(asyncore.dispatcher):
        def __init__(self, host, port):
                asyncore.dispatcher.__init__(self)
                self.host = host
                self.port = port
                self.status = 0
                self.rbuffer = ''
                self.sbuffer = ''
                self.callDict = {0x0a00 : self.onVerify, \
                                                 0x1000 : self.onInitMarket, \
                                                 0x0100 : self.onTrace, \
                                                 0x0400 : self.onReal, \
                                                 0x0c00 : self.onTextInfo}
                self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
                self.connect((host, port))

        def handle_connect(self):
                print ''.join(['已连接', self.host, ':', str(self.port)])

---------------------------------
class itquote_trd(threading.Thread):
        def __init__(self, threadname, sock):
                threading.Thread.__init__(self, name = threadname)

        def run(self):
                print '异步线程启动'
                asyncore.loop()
                print '异步线程停止'

----------------------------------
sock = itsocket.itsocket(ihost, iport)

论坛徽章:
0
2 [报告]
发表于 2007-07-30 14:06 |只看该作者
用法不对,loop()是全局的,不要放在线程中。
http://www.chmhome.com/Python_St ... l-chp-7-sect-4.html

论坛徽章:
0
3 [报告]
发表于 2007-07-30 14:46 |只看该作者

移到主线程中还是不行

在if __name__ == '__main__':下面加上这句话,还是没有回调handle_connect

论坛徽章:
0
4 [报告]
发表于 2007-07-30 15:06 |只看该作者
上面给出链接是<<python standard library>>对asyncore的举例,简化了一下


  1. import asyncore
  2. import socket

  3. class Request(asyncore.dispatcher):

  4.     def __init__(self, host, port=80):
  5.         asyncore.dispatcher.__init__(self)
  6.         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  7.         self.connect((host, port))

  8.     def writable(self):
  9.         return 0 # don't have anything to write

  10.     def handle_connect(self):
  11.         print "connected"

  12.     def handle_expt(self):
  13.         pass

  14.     def handle_read(self):
  15.         self.close()
  16.         pass

  17.     def handle_close(self):
  18.         self.close()

  19. request = Request("www.163.com", 80)
  20. asyncore.loop()
复制代码

论坛徽章:
0
5 [报告]
发表于 2007-07-30 15:17 |只看该作者
4楼的例子运行以后屏幕上没有任何显示,是不是要装第三方包?我是windows环境

论坛徽章:
0
6 [报告]
发表于 2007-07-30 15:26 |只看该作者
囧掉了,楼上用的什么版本,我的python 2.5.1,正常。

论坛徽章:
0
7 [报告]
发表于 2007-07-30 15:54 |只看该作者
一样的251
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP