I've implemented simple notification listener with psycopg2, which will listen trigger events from PostgreSQL table changes:
def trigger_event(self, connection) -> List[dict]:
cursor = connection.cursor()
cursor.execute("LISTEN mychannel")
while True:
select.select([connection], [], [])
connection.poll()
while connection.notifies:
notify = connection.notifies.pop()
yield notify.payload
for payload in self.trigger_event(connection):
print(payload)
When I start this code, it works properly. However, when I try to it, it does not listen keyboard interrupts or any signal, or termination takes very long time.
Is it possible to make Psycopg2 notification listening stop without any hard process killing?