使用 PyZMQ s IOLooop 实例时, 我有一种奇怪的系统行为:
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 的 zmq 端口, 供 JSON 请求, 并在文件可用时读取文件的内容( 并且当它可用时, 将它操作并折回它。 基本上, 文件是一个为此而创建的特殊 / proc/ 内核模块 ) 。
一切都很好,但是,由于某种原因, 当看脚步时,我看到以下各点:
...
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线之间完成。
有什么想法吗?