I find it unlikely this program works as expected. You have written approximately:
- Open a database connection, & assign that to
client - Get
write_apifromclient - Define a function which (at some point in the future) uses
write_api - Call
client.close()due to the exit of thewithstatement! - Ask another function to handle messages by calling your defined function which uses an api on a “now closed”
client!
As you can see, the with statement is not doing anything but giving you a false sense of security. You need to either:
- open a new database connection inside
handle_message, or - indent the subsequent code such that
clientis not closed until after yourwhile True:loop exits (presumably by a KeyboardInterruptException`)