asyncore.py

asyncore库是python的一个标准库,它是一个异步socket的包装。我们操作网络的时候可以直接使用socket等底层的库,但是asyncore使得我们可以更加方便的操作网络,避免直接使用socket,select,poll等工具时需要面对的复杂。

这个库很简单,包含了一个函数和一个类
* loop()函数
* dispatcher基类
需要注意的是,loop函数是全局的,不是dispatcher的方法

每一个从dispatcher继承的类的对象,都可以看作我们需要处理的一个socket,可以是TCP连接或者UDP,甚至是其它不常用的。使用容易,我们需要定义一个类,它继承dispatcher,然后我们重写(覆盖)一些方法就可以了。

我们需要重写的方法一般都以handle_打头。
class refuse(dispatcher):

def handle_accept():
#do nothing ...
pass

loop()函数负责检测一个dict,dict中保存dispatcher的实例,这个字典被称为channel。每次创建一个dispatcher对象,都会把自己加入到一个默认的dict里面去(当然也可以自己指定channel)。当对象被加入到channel中的时候,socket的行为都已经被定义好,程序只需要调用loop(),一切功能就实现了。

asyncore是python标准库中的一个良好的设计
在python的标准文档中,有一个asyncore的例子
import asyncore, socket


class http_client(asyncore.dispatcher):

def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect( (host, 80) )
self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path

def handle_connect(self):
pass

def handle_close(self):
self.close()

def handle_read(self):
print self.recv(8192)

def writable(self):
return (len(self.buffer) > 0)

def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]

c = http_client('www.python.org', '/')

asyncore.loop()

运行这个函数,发现python.org的首页被下载下来了,也就是说我们实现了一个http层的协议?但是我们用的仅仅是socket级别的API...那么来看看这几行代码的奥妙吧!

writable和readable在检测到一个socket可以写入或者检测到数据到达的时候,被调用,并返回一个bool来决定是否handle_read或者handle_write

打开asyncore.py可以看到,dispatcher类中定义的方法writable和readable的定义相当的简单:
    def readable(self):

return True

def writable(self):
return True

就是说,一旦检测到可读或可写,就直接调用handle_read/handle_write,但是在上面的例子中,我们却看到了一个重载(看上去像C++的虚函数,不是吗?)
    def writable(self):

return (len(self.buffer) > 0)

很明显,当我们有数据需要发送的时候,我们才给writable的调用者返回一个True,这样就不需要在handle_write中再做判断了,逻辑很明确,代码很清晰,美中不足的是理解需要一点时间,但是不算困难吧!

其余的代码看起来就很清晰了,有一种并来将挡的感觉。当一个http服务器发送处理完成你的请求,close socket的时候,我们的handle_close()也相应完成自己的使命。close()将对象自身从channel中删除,并且负责销毁socket对象。
    def close(self):

self.del_channel()
self.socket.close()

loop()函数检测到一个空的channel,将退出循环,程序完成任务,exit。
你给到代码出现了: --- Logging error --- Traceback (most recent call last): File "D:\Develop\Python\Python38\lib\logging\__init__.py", line 1088, in emit stream.write(msg + self.terminator) TypeError: a bytes-like object is required, not 'str' Call stack: File "1.py", line 244, in <module> server = mysql_listener() File "1.py", line 231, in __init__ log.info("Listening on port %d", PORT) Message: 'Listening on port %d' Arguments: (3306,) --- Logging error --- Traceback (most recent call last): File "D:\Develop\Python\Python38\lib\logging\__init__.py", line 1088, in emit stream.write(msg + self.terminator) TypeError: a bytes-like object is required, not 'str' Call stack: File "1.py", line 247, in <module> asyncore.loop() File "D:\Develop\Python\Python38\lib\asyncore.py", line 203, in loop poll_fun(timeout, map) File "D:\Develop\Python\Python38\lib\asyncore.py", line 150, in poll read(obj) File "D:\Develop\Python\Python38\lib\asyncore.py", line 83, in read obj.handle_read_event() File "D:\Develop\Python\Python38\lib\asyncore.py", line 414, in handle_read_event self.handle_accept() File "1.py", line 239, in handle_accept log.info('Connection from: %r', pair[1]) Message: 'Connection from: %r' Arguments: (('127.0.0.1', 55143),) --- Logging error --- Traceback (most recent call last): File "1.py", line 199, in found_terminator raise LastPacket() LastPacket During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\Develop\Python\Python38\lib\logging\__init__.py", line 1088, in emit stream.write(msg + self.terminator) TypeError: a bytes-like object is required, not 'str' Call stack: File "1.py", line 247, in <module> asyncore.loop() File "D:\Develop\Python\Python38\lib\asyncore.py", line 203, in loop poll_fun(timeout, map) File "D:\Develop\Python\Python38\lib\asyncore.py", line 150, in poll read(obj) File "D:\Develop\Python\Python38\lib\asyncore.py", line 83, in read obj.handle_read_event() File "D:\Develop\Python\Python38\lib\asyncore.py", line 420, in handle_read_event self.handle_read() File "D:\Develop\Python\Python38\lib\asynchat.py", line 151, in handle_read self.found_terminator() File "1.py", line 205, in found_terminator log.info('Last packet') Message: 'Last packet' Arguments: () 这些错误
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值