0

We have a system consisting of two c++ applications (running as exes) and a plotly dash dashboard in python. It is an offline system (dash running on local host), using an sqlite database (in WAL mode) to store data and to interact between the different components.

Every C++ component has an open database connection that is passed through the respective program and this works fine.

Now my question is how to handle this on the python/dash side, since I do not understand dash enough (I'm not the dash programmer).

  • In which place would I allocate the database connection object to pass it around or to make it available from the different callbacks.
  • Does dash on local host use multi threading for its callbacks etc where a single database connection in the application would be a problem
  • In case of multi threading problems: Would it be a valid solution to open a database connection in every callback (when necessary) and close it explicitly before leaving the callback?
LCsa
  • 607
  • 9
  • 30

1 Answers1

1

The most performant approach would probably be to reuse the connection between callbacks. However, as you note, with this approach you must be careful if you use multiple threads/processes. A possible design would be to use thread local connection pools, but for your use case that might be overkill. The overhead of opening/closing an sqlite connection is of the order of tens of micro seconds, so it probably won't affect performance much. But I would advise to test it out.

The number of threads/processes used by Dash depends on the configuration of the underlying Flask server. If you set threaded=True, each request will be handled in a separate thread. Using the development server, you can pass the argument directly, i.e.

app.run_server(threaded=True)

Alternatively, you can pass processes=X with X > 1 to use multiple processes.

emher
  • 5,634
  • 1
  • 21
  • 32
  • Thank you for the quick response! The database accesses will be in the order of 500-2500 ms, so not very time critical, if that helps for your answer... What is a "request" in the dash context? Would you recommend to turn `threading=False`? Thanks! – LCsa Jul 26 '21 at 18:17
  • In general, I would recommend to use a real production server (e.g. gunicorn) with a few workers. But for your use case, the development server with threaded=True (the default) might do. In Dash, a new request is made for each (Python) callback. – emher Jul 27 '21 at 04:42
  • 1
    the major problem was that I put the closing of the database connection into the class's destructor, which is never a good idea in python. Once I had an explicit close() call in every callback, the problem disappeared as well – LCsa Aug 20 '21 at 11:32
  • That makes sense. In general, since the server in Dash is assumed stateless, classes should be used with caution. – emher Aug 20 '21 at 17:06