An open source multi-tool for exploring and publishing data https://datasette.io
  • Python 88.4%
  • HTML 7.5%
  • JavaScript 2.4%
  • CSS 1.2%
  • Shell 0.3%
  • Other 0.1%
Find a file
Simon Willison 31b21f5c5e Moved all SQLite queries to threads
SQLite operations are blocking, but we're running everything in Sanic, an
asyncio web framework, so blocking operations are bad - a long-running DB
operation could hold up the entire server.

Instead, I've moved all SQLite operations into threads. These are managed by a
concurrent.futures ThreadPoolExecutor. This means I can run up to X queries in
parallel, and I can continue to queue up additional incoming HTTP traffic
while the threadpool is busy.

Each thread is responsible for managing its own SQLite connections - one per
database. These are cached in a threadlocal.

Since we are working with immutable, read-only SQLite databases it should be
safe to share SQLite objects across threads. On this assumption I'm using the
check_same_thread=False option. Opening a database connection looks like this:

    conn = sqlite3.connect(
        'file:filename.db?immutable=1',
        uri=True,
        check_same_thread=False,
    )

The following articles were helpful in figuring this out:

* https://pymotw.com/3/asyncio/executors.html
* https://marlinux.wordpress.com/2017/05/19/python-3-6-asyncio-sqlalchemy/

Closes #45. Refs #38.
2017-11-04 19:21:44 -07:00
datasite Moved all SQLite queries to threads 2017-11-04 19:21:44 -07:00
.gitignore Implemented multi-db support plus initial URL structure 2017-10-23 19:00:37 -07:00
.travis.yml Configured Travis CI 2017-11-04 16:47:46 -07:00
Dockerfile Started work on cli, which also meant adding setup.py 2017-10-27 00:08:24 -07:00
LICENSE Initial commit 2017-10-22 17:39:03 -07:00
setup.cfg python setup.py test now runs the tests 2017-11-04 16:40:27 -07:00
setup.py Default subcommand is now serve 2017-11-04 16:53:50 -07:00
test_helpers.py python setup.py test now runs the tests 2017-11-04 16:40:27 -07:00