gpt4 book ai didi

c++ - cppzmq : Is it possible to get a global context without creating one as a global variable?

转载 作者:行者123 更新时间:2023-11-30 04:42:42 26 4
gpt4 key购买 nike

我的用例通常只涉及一个全局上下文。据我所知,pyzmq 有一个 API zmq.Context.instance() 来获取全局单例,但我找不到等效的 cppzmq API。

是否可以访问全局上下文而无需显式创建和跟踪它?

最佳答案

Q : Is it possible to have access to a global context without having to create it explicitly and track it?

不,至少不是没有当前 cppzmq 的主要重新设计工作

原因:

PyZMQ 最近开始与普通的 python 语言绑定(bind)不同,以使用已发布的通用 ZeroMQ API。

More Than Just Bindings

PyZMQ is ostensibly the Python bindings for ØMQ, but the project, following Python’s ‘batteries included’ philosophy, provides more than just Python methods and objects for calling into the ØMQ C++ library. The Core as Bindings

PyZMQ is currently broken up into four subpackages. First, is the Core. zmq.core contains the actual bindings for ZeroMQ, and no extended functionality beyond the very basic. The core modules are split, such that each basic ZeroMQ object (or function, if no object is associated) is a separate module, e.g. zmq.core.context contains the Context object, zmq.core.poll contains a Poller object, as well as the select() function, etc. ZMQ constants are, for convenience, all kept together in zmq.core.constants.

There are two reasons for breaking the core into submodules: recompilation and derivative projects. The monolithic PyZMQ became quite tedious to have to recompile everything for a small change to a single object. With separate files, that’s no longer necessary. The second reason has to do with Cython. PyZMQ is written in Cython, a tool for efficiently writing C-extensions for Python. By separating out our objects into individual pyx files, each with their declarations in a pxd header, other projects can write extensions in Cython and call directly to ZeroMQ at the C-level without the penalty of going through our Python objects.

Thread Safety

In ØMQ, Contexts are threadsafe objects, but Sockets are not. It is safe to use a single Context (e.g. via zmq.Context.instance()) in your entire multithreaded application, but you should create sockets on a per-thread basis. If you share sockets across threads, you are likely to encounter uncatchable c-level crashes of your application unless you use judicious application of threading.Lock, but this approach is not recommended.

努力工作的 ZeroMQ 极客们已经注意到,原始的 ZeroMQ API(从 v2.1.11+ 开始)还有其他建议:

1) 应用程序必须显式创建至少一个Context实例(并稍后终止它)

2) Context-instance(s) 可以在线程之间自由共享,无需 Context 需要任何锁定-服务调用方

3) 即使将 Socket-instance(s) 传递(而不是迁移)到新创建的线程(即由不属于“foreign”-Context 的线程维护),也授予线程安全性 ),PyZMQ 文档从中明确警告并分散用户的注意力。

关于c++ - cppzmq : Is it possible to get a global context without creating one as a global variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58638564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com