English 中文(简体)
ZMQ IOLooop 实例写入/阅读工作流程
原标题:ZMQ IOLoop instance write/read workflow

使用 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线之间完成。

有什么想法吗?

最佳回答

似乎您正在遇到一个缓存问题。 如果有些文件与对象相似, 您可以尝试在它上明确调用. flush (), ZMQ Socket 也会使用同样的方法, 因为它也可以为效率原因持有信件 。

当某些文件引用被垃圾收集时, 文件内容会被冲洗 。

额外:

使用较新版本的 Python 提供打开的上下文管理器逻辑()

with open("my_file") as some_file:
     some_file.write("blah")

一旦完成此上下文, 某些文件将自动冲洗并关闭 。

问题回答

暂无回答




相关问题
Curve fitting with lmfit for Mossbauer spectroscopy

Im pretty new to lmfit and I keep running into this error. My dataset is pretty large ~27000 points of data from I gather: my error msg import numpy as np import matplotlib.pyplot as plt from lmfit ...

Python SocketServer: sending to multiple clients?

Well, I m trying to build a small python prgram with a SocketServer that is supposed to send messages it receives to all connected clients. I m stuck, I don t know how to store clients on the ...

Is there no mysql connector for python 2.7 on windows

While I see a bunch of links/binaries for mysql connector for python 2.6, I don t see one for 2.7 To use django, should I just revert to 2.6 or is there a way out ? I m using windows 7 64bit django ...

How to update the image of a Tkinter Label widget?

I would like to be able to swap out an image on a Tkinter label, but I m not sure how to do it, except for replacing the widget itself. Currently, I can display an image like so: import Tkinter as ...

PyScripter for Python 2.7

How to run PyScripter if you have Python 2.7 installed? There is a command line parameter for Pyscipter to tell it which pythonXX.dll to use, but I can t get this working.

How to set time limit on raw_input

in python, is there a way to, while waiting for a user input, count time so that after, say 30 seconds, the raw_input() function is automatically skipped?

热门标签