cppzmq:是否可以在不创建一个全局变量的情况下获得一个全局上下文?

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

我的用例通常只涉及一个全局上下文。 AFAIK,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 语言绑定不同 以使用已发布的通用 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-实例可以在线程之间自由共享 Context- 不需要任何锁定服务调用方

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