This is a simple chat-like program using pub-sub pattern, backed by PostgreSQL's LISTEN/NOTIFY command.
publish message to foo
channel from user nickname
.
$ python pub.py foo nickname
PUBLISH to channel #foo
test message
what's up?
<Ctrl-D>
$
publish message to bar
channel from user mike
.
$ python pub.py bar mike
PUBLISH to channel #bar
pub/sub rules!
<Ctrl-D>
$
subscribe to foo
or bar
channel.
$ python sub.py foo bar
SUBSCRIBE TO channel #foo
SUBSCRIBE TO channel #bar
#foo - [nickname]:test message
#foo - [nickname]:what's up?
#bar - [mike]:pub/sub rules!
<Ctrl-C>
UNSUBSCRIBE FROM channel #foo
UNSUBSCRIBE FROM channel #bar
$
Your code showed me a true path, it is outdated for python 3.6 already but there is a pgpubsub module now (which by itself uses select inside). I use this approach to deliver news events and notficiation to chat server clients via websocket from database (chat server handles its own clients and messages with websocket only):