豆瓣
扫码直接下载
在读 ZeroMQ
If there's one lesson we've learned from 30+ years of concurrent programming, it is: just don't share state. You should follow some rules to write happy multithreaded code with ZeroMQ: - Isolate data privately within its thread and never share data in multiple threads. The only exception to this are ZeroMQ contexts, which are threadsafe. - Stay away from the classic concurrency mechanisms like as mutexes, critical sections, semaphores, etc. These are an anti-pattern in ZeroMQ applications. - Create one ZeroMQ context at the start of your process, and pass that to all threads that you want to connect via inproc sockets. - Use attached threads to create structure within your application, and connect these to their parent threads using PAIR sockets over inproc. The pattern is: bind parent socket, then create child thread which connects its socket. - Use detached threads to simulate independent tasks, with their own contexts. Connect these over tcp. Later you can move these to stand-alone processes without changing the code significantly. - All interaction between threads happens as ZeroMQ messages, which you can define more or less formally. - Don't share ZeroMQ sockets between threads. ZeroMQ sockets are not threadsafe. Technically it's possible to migrate a socket from one thread to another but it demands skill. The only place where it's remotely sane to share sockets between threads are in language bindings that need to do magic like garbage collection on sockets.引自 Multithreading with ZeroMQ inproc 的 socket 看起来比较像 golang 的 channel;里面提到 "attached thread" / "PAIR sockets over inproc." 目测用起来会比较像协程或者管道。 ZeroMQ uses native OS threads rather than virtual "green" threads. The advantage is that you don't need to learn any new threading API, and that ZeroMQ threads map cleanly to your operating system. You can use standard tools like Intel's ThreadChecker to see what your application is doing.引自 Multithreading with ZeroMQ gevent 这类“绿色线程”的协程库太 overrate 了,不过 python 生态圈也没有什么其他选择。
inproc 的 socket 看起来比较像 golang 的 channel;里面提到 "attached thread" / "PAIR sockets over inproc." 目测用起来会比较像协程或者管道。
gevent 这类“绿色线程”的协程库太 overrate 了,不过 python 生态圈也没有什么其他选择。
> 红色有角F叔的所有笔记(699篇)
We said that ØMQ does I/O in a background thread. One I/O thread (for all sockets) i...
ZeroMQ's error handling philosophy is a mix of fail-fast and resilience. Processes, we ...
You should think about using zero-copy in the specific case where you are sending large...
ZeroMQ uses the concept of HWM (high-water mark) to define the capacity of its internal...
表示其中内容是对原文的摘抄