我有一些使用AF_UNIX套接字asyncore的問題。此代碼蟒蛇asyncore與AF_UNIX套接字的問題
import asyncore, socket, os
class testselect(asyncore.dispatcher):
path = '/tmp/mysocket'
def __init__(self):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.bind(self.path)
self.buffer = 'buffer'
def handle_connect(self):
print 'handle_connect'
pass
def handle_close(self):
print 'handle_close'
if os.path.exists(self.path)
os.remove(self.path)
self.close()
def handle_read(self):
print 'handle_read'
print self.recv(8192)
def writable(self):
print 'writable'
return (len(self.buffer) > 0)
def handle_write(self):
print 'handle_write'
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
client = testselect()
asyncore.loop()
如果我執行的代碼
$ python select_prova.py
writable
handle_connect
handle_write
handle_close
$
它立刻退出,而不會等待讀取和寫入。如果我改變代碼強制寫()方法返回總是False
,它正確地等待輸入,我可以用socat這樣
$ socat readline UNIX:/tmp/mysocket
但僅讀(寫溝通邏輯不工作,因爲可寫的()返回False
)。我的代碼中是否有錯誤,或者我無法使用asyncore/select()管理AF_UNIX套接字?
這是一個非常有趣的難題,你有。 – Omnifarious 2011-05-13 16:34:52