Skip to main content

A thread safe queue worker that executes SQL for multi-threaded applications.

Project description

Sqlite3 Execution Queue

version license pyversions
donate powered made

A thread safe queue worker that executes SQL for multi-threaded applications.

Hierarchy

sqlqueue
'---- SqlQueue()
    |---- sql()
    |---- _sql()
    |---- commit()
    '---- stop()

Example

python

from sqlq import *

# specify the db file, relative or absolute path
# set server=True
sqlqueue = SqlQueue(server=True, db="db.db")

# SQL execution modes
# all will return the executed SQL result immediately
SqlQueue(server=True, db="default", timeout_commit=1000).sql("SELECT * FROM table;")
SqlQueue(server=True, db="commit per 1ms", timeout_commit=1).sql("INSERT INTO table VALUES (?);", (0,))
SqlQueue(server=True, db=r"C:\somewhere\db.db").sql("INSERT INTO table VALUES (?);", ((0,),(0,)))
SqlQueue(server=True, db="../../data/db.db").sql('''
CREATE TABLE "tablea" ("a" TEXT);
DELETE TABLE "table";
''')

# stop the worker
# use it at your own risk
# otherwise data will be lost
# always commit before stopping it
sqlqueue.commit()
sqlqueue.stop()

# using sqlq with encryptedsocket
# server
from encryptedsocket import SS
from easyrsa import *
sqlqueueserver = SqlQueue(server=True, db=r"db.db")
threading.Thread(target=SS(EasyRSA(bits=1024).gen_key_pair(), sqlqueueserver.functions).start).start()
# client
sqlqueue = SqlQueue()
for i in range(10):
    # SqlQueue._sql() must not be used in socket mode
    sqlqueue.sql("INSERT INTO test VALUES (?);", (str(i)))
# server
sqlqueueserver.commit()
sqlqueueserver.stop()

# SQL execution speed
# # server mode _sql() without built-in ThreadWrapper() handler
# SqlQueue(server=True, ...)._sql() <
# # server mode sql() with built-in ThreadWrapper() handler
# SqlQueue(server=True, ...).sql() <
# # client mode sql() with two nested ThreadWrapper() handlers
# SqlQueue().sql()

# this example shows how sqlq is used
# SQL should not be executed frequently
r = (1, 5, 10, 50, 100, 200)
r = (50,)
for l in r:
    tw = ThreadWrapper(threading.Semaphore(l))
    starttime = time.time()
    result = {}  # result pool
    for i in range(l):
        def job(i):
            # return SQL execution result to result pool
            return sqlqueue._sql(threading.get_ident(), "INSERT INTO test VALUES (?);", (str(i),))
        tw.add(job, args=args(i), result=result, key=i)  # pass the pool and uid in
    tw.wait()
    # p(result)
    p(l, (time.time()-starttime)/l, time.time()-starttime)
    p(sqlqueue.sql("SELECT * FROM test;"))
    tw = ThreadWrapper(threading.Semaphore(l))
    starttime = time.time()
    for i in range(l):
        def job(i):
            sqlqueue._sql(threading.get_ident(), f"DELETE FROM test WHERE a = ?;", (str(i),))
        tw.add(job, args=args(i))
    tw.wait()
    p(l, (time.time()-starttime)/l, time.time()-starttime)
    # in order to use SqlQueue()._sql(), ThreadWrapper() is
    # recommended to queue threads, check threadwrapper for more info
    # SqlQueue()._sql() will raise execution error
    # it will only report it to the result
    # you should handle the errors separately


    starttime = time.time()
    for i in range(l):
        sqlqueue.sql("INSERT INTO test VALUES (?);", (str(i),))
    p(l, (time.time()-starttime)/l, time.time()-starttime)
    p(sqlqueue.sql("SELECT * FROM test;"))
    starttime = time.time()
    for i in range(l):
        sqlqueue.sql(f"DELETE FROM test WHERE a = ?;", (str(i),))
    p(l, (time.time()-starttime)/l, time.time()-starttime)
    p()
    sqlqueue.commit()  # manual commit
    # both manual and timeout commit always wait until
    # the current SQL execution is completed. 
    # the worker will not raise any error
    # however SqlQueue().sql() will re-raise execution error

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlq-0.3.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

sqlq-0.3.0-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlq-0.3.0.tar.gz.

File metadata

  • Download URL: sqlq-0.3.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.0

File hashes

Hashes for sqlq-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cc6b50494f707fa64305866695b626958bd01aadf72f43afbd9b8a39ec27c5f7
MD5 6611a910e225114baabbb54f7672540f
BLAKE2b-256 c683e78b5502b09394ec2c1e3b62ea70eaba3ba7fa25517faab1629f91fd23b6

See more details on using hashes here.

File details

Details for the file sqlq-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: sqlq-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.0

File hashes

Hashes for sqlq-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aeefec41255830ca1cbe38b9f0ddbf43b1edc8d381f2643499aad98bcc3489c5
MD5 ee50826b3e0c1b160a04d0c1a2fccd1f
BLAKE2b-256 9af06ca33871bd24f5b27f3e44ca23fc1c6b7204290696c1ac0c68b3c9fb1eb2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page