3
我在使用PyZMQ的IOLoop例如,當一個奇怪的系統行爲:ZMQ IOLoop例如寫入/讀取的工作流程
def main():
context = zmq.Context()
s = context.socket(zmq.REP)
s.bind('tcp://*:12345')
stream = zmqstream.ZMQStream(s)
stream.on_recv(on_message)
io_loop = ioloop.IOLoop.instance()
io_loop.add_handler(some_file.fileno(), on_file_data_ready_read_and_then_write, io_loop.READ)
io_loop.add_timeout(time.time() + 10, another_handler)
io_loop.start()
def on_file_data_ready_read_and_then_write(fd, events):
# Read content of the file and then write back
some_file.read()
print "Read content"
some_file.write("blah")
print "Wrote content"
def on_message(msg):
# Do something...
pass
if __name__=='__main__':
main()
基本上事件循環監聽ZMQ的12345爲JSON請求的端口,並讀取內容從一個文件當可用時(以及何時可用),操作它並將其返回。基本上,該文件是專門爲此構建的/ proc/kernel模塊。
一切都運行良好,但出於某些原因看strace的我看到下面的時候:
...
1. read(\23424) <--- Content read from file
2. write("read content")
3. write("Wrote content")
4. POLLING
5. write(\324324) # <---- THIS is the content that was sent using some_file.write()
...
所以好像在寫文件沒有在python腳本的順序進行,但寫入該文件的系統調用是在輪詢後完成的,即使它應該在第2行和第3行之間完成。
任何想法?