Implement a Thread-Safe Pub-Sub Service
Hot Agent StartupSoftware Engineer · OnsiteLast updated
This question evaluates concurrent programming and thread-safety skills within the Software Engineering Fundamentals domain, focusing on synchronization primitives, message delivery semantics, and coordination between publishers and subscribers at a practical application level.
Implement a simple server-side publish-subscribe system using Python's standard threading library.
Requirements:
- Multiple publisher threads may publish messages concurrently.
- Multiple subscriber threads may subscribe to topics concurrently.
- A subscriber can subscribe to and unsubscribe from a topic.
- When a message is published to a topic, all currently subscribed subscribers should receive the message.
- The implementation must avoid race conditions and deadlocks.
- Use Python threading primitives such as
Lock,RLock,Condition,Event, and thread-safe queues where appropriate. - Discuss delivery semantics, shutdown behavior, and how to avoid one slow subscriber blocking all publishers.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.