Skip to main content
1 of 2
AJNeufeld
  • 35.3k
  • 5
  • 41
  • 103

I find it unlikely this program works as expected. You have written approximately:

  1. Open a database connection, & assign that to client
  2. Get write_api from client
  3. Define a function which (at some point in the future) uses write_api
  4. Call client.close() due to the exit of the with statement!
  5. 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 client is not closed until after your while True: loop exits (presumably by a KeyboardInterruptException`)
AJNeufeld
  • 35.3k
  • 5
  • 41
  • 103