事件期间ZMQ上下文可以切换吗? [英] Can ZMQ context switch during an event?

查看:60
本文介绍了事件期间ZMQ上下文可以切换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用2个异步函数创建一个Python生产者和使用者.其中一个函数是从服务器(生产者)获取数据的回调,另一个是定时回调,每N秒执行一次(消费者).我正在尝试为此使用ZMQ/Reactor,因为我想避免计时器线程的开销,而仅使用事件循环.ZMQ/Reactor事件循环是否按到达顺序执行每个事件,还是有可能随时将使用者功能上下文切换到生产者功能?

I am trying to create a Python producer and consumer using 2 asynchronous functions. 1 of the functions is a callback getting data from server (producer), the other is a timed callback that executes every N seconds (consumer). I am trying to use ZMQ/Reactor for this as I would like to avoid overhead of a timer thread and just use the event loop. Does the ZMQ/Reactor event loop execute each event in order they arrive or is there a chance that the consumer function could be context switched to the producer function at any time?

简化示例:
主要问题是我无法从列表中弹出所有项目并自动清除

Simplified Example:
The main issue is that I cannot pop all items from a list and clear it atomically

queue = []
def producer():
   # gets data from server
   queue.append(single_record)

def consumer():
   records = list(queue)
   # inserts records into db
   queue.clear()

推荐答案

Q :事件期间ZMQ上下文可以切换吗?"

好吧,让我们从零禅宗的祖先开始-彼得·辛琴斯(Pieter HINTJENS)和马丁·苏斯特里克(Martin SUSTRIK)的体系结构完全消除了接收消息的任何此类保证,获得所有"em"的保证就越少.

Well,
let's start from the fathers of the Zen of Zero - you were never promised by Pieter HINTJENS' & Martin SUSTRIK's architecture evengelisation any such warranty of receiving a message at all, the less a warranty to get all of 'em the less "in order".

Zen-of-Zero中只有一项保修:如果我们确实做一条 .recv 消息,则肯定是原始消息的二进制相同副本,即被调度为通过ZeroMQ发送.

There is one and only one warranty in the Zen-of-Zero : if we indeed do .recv a message, it is sure to be a binary-identical replica of the original one, that was dispatched to be sent over ZeroMQ.

ZeroMQ Context() -instance是一个独立的异步MQ引擎,这一事实无济于事,并且知道Python每转换一百个伪代码,其内部GIL锁所有权即可.Py2.x中的说明或Py3.+中的每个5 [ms]都不能帮助您明确表示需要获得任何此类保证,对吗?

The fact that the ZeroMQ Context()-instance is an independent, asynchronous MQ-engine, does not help in this and knowing that Python switches its internal GIL-lock ownership each hundred of pseudo-instructions in Py2.x or each 5[ms] in Py3.+ does not help your expressed need to get any such warranty either, does it?

您还需要其他一些附加机制,以弥补Python的空白,并在零禅的领土上架起缺失的保证.

You need some other add-on mechanics, that glue-in the gaps of Python and that bridge the missing warranties over the territories of Zen-of-Zero.

这篇关于事件期间ZMQ上下文可以切换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆