Skip to content

Commit f482ab1

Browse files
author
Abhishek Singh
committed
v1.0.2
1. Updated Readme. 2. Client shutdown method is called cleanup
1 parent baf6069 commit f482ab1

7 files changed

Lines changed: 67 additions & 21 deletions

File tree

README.md

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# sqlite_rx [![Downloads](https://pepy.tech/badge/sqlite-rx)](https://pepy.tech/project/sqlite-rx) ![sqlite-rx](https://github.com/aosingh/sqlite_rx/actions/workflows/sqlite_build.yaml/badge.svg)
1+
# sqlite_rx
2+
[![PyPI version](https://badge.fury.io/py/sqlite-rx.svg)](https://pypi.python.org/pypi/sqlite-rx) [![sqlite-rx](https://github.com/aosingh/sqlite_rx/actions/workflows/sqlite_build.yaml/badge.svg)](https://github.com/aosingh/sqlite_rx/actions) [![Downloads](https://pepy.tech/badge/sqlite-rx)](https://pepy.tech/project/sqlite-rx)
23

3-
[![PyPI version](https://badge.fury.io/py/sqlite-rx.svg)](https://pypi.python.org/pypi/sqlite-rx)
44

55
[![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)]((https://www.python.org/downloads/release/python-370/)) [![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/) [![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)
66
[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)]((https://www.python.org/downloads/release/python-390/))
@@ -11,7 +11,7 @@
1111
[SQLite](https://www.sqlite.org/index.html) is a lightweight database written in C.
1212
The Python programming language has in-built support to interact with the database (locally) which is either stored on disk or in memory.
1313

14-
## sqlite_rx
14+
## Introduction
1515
With `sqlite_rx`, clients should be able to communicate with an `SQLiteServer` in a fast, simple and secure manner and execute queries remotely.
1616

1717
Key Features
@@ -20,7 +20,7 @@ Key Features
2020
- Authentication using [ZeroMQ Authentication Protocol (ZAP)](https://rfc.zeromq.org/spec:27/ZAP/)
2121
- Encryption using [CurveZMQ](http://curvezmq.org/)
2222
- Generic authorization policy during server startup
23-
- Schedule regular backups for on-disk database (Not supported on Windows and Python version < 3.7)
23+
- Schedule regular backups for on-disk database (Not supported on Windows and for Python versions older than 3.7)
2424

2525

2626
# Install
@@ -38,8 +38,6 @@ pip install sqlite_rx
3838
- CPython 3.6, 3.7, 3.8, 3.9
3939
- PyPy3.6
4040

41-
# Examples
42-
4341
## Server
4442

4543
`SQLiteServer` runs in a single thread and follows an event-driven concurrency model (using `tornado's` event loop) which minimizes the cost of concurrent client connections.
@@ -66,8 +64,8 @@ def main():
6664

6765
if __name__ == '__main__':
6866
main()
69-
```
7067

68+
```
7169

7270
## Client
7371

@@ -84,8 +82,6 @@ The `execute` method reacts to the following keyword arguments:
8482
4. `retries`: Number of times to retry before abandoning the request. Default is 5
8583

8684

87-
Below are a few examples
88-
8985
### Instantiate a client
9086

9187
```python
@@ -101,7 +97,7 @@ logging.config.dictConfig(get_default_logger_settings(logging.DEBUG))
10197
client = SQLiteClient(connect_address="tcp://127.0.0.1:5000")
10298
```
10399

104-
### CREATE TABLE statement
100+
### CREATE TABLE
105101

106102
```python
107103
result = client.execute("CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)")
@@ -113,7 +109,6 @@ pprint(result)
113109
'items': []}
114110
```
115111

116-
117112
### INSERT MANY rows
118113

119114
```python
@@ -246,6 +241,59 @@ pprint(result)
246241
'items': []}
247242
```
248243

244+
### Client Clean up
245+
246+
Call `cleanup()`
247+
248+
```python
249+
client = SQLiteClient(connect_address="tcp://127.0.0.1:5001")
250+
args = ('IBM',)
251+
result = client.execute("SELECT * FROM stocks WHERE symbol = ?", *args)
252+
client.cleanup()
253+
```
254+
255+
Use `with` contextmanager
256+
257+
```python
258+
259+
client = SQLiteClient(connect_address="tcp://127.0.0.1:5001")
260+
args = ('IBM',)
261+
with client:
262+
result = client.execute("SELECT * FROM stocks WHERE symbol = ?", *args)
263+
264+
```
265+
266+
## Backup
267+
268+
With `sqlite-rx`, database backup can be regularly peformed. The backup function can be configured to run as a background thread during server startup. Use `backup_interval` argument to specify how frequently backup should be performed in seconds.
269+
270+
```python
271+
272+
def main():
273+
274+
# database is a path-like object giving the pathname
275+
# of the database file to be opened.
276+
277+
# You can use ":memory:" to open a database connection to a database
278+
# that resides in RAM instead of on disk
279+
280+
logging.config.dictConfig(get_default_logger_settings(logging.DEBUG))
281+
server = SQLiteServer(database="main.db",
282+
bind_address="tcp://127.0.0.1:5000",
283+
backup_database='backup.db',
284+
backup_interval=500)
285+
server.start()
286+
server.join()
287+
288+
if __name__ == '__main__':
289+
main()
290+
291+
```
292+
### Constrinats
293+
- Requires Python >= 3.7
294+
- Backup is performed using sqlite backup API which was introduced in Python 3.7
295+
- Not supported on Windows
296+
249297
## Generic Default Authorization Policy
250298

251299

sqlite_rx/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def execute(self,
163163
return response
164164
else:
165165
LOG.warning("No response from server, retrying...")
166-
self.shutdown()
166+
self.cleanup()
167167
request_retries -= 1
168168
if request_retries == 0:
169169
LOG.error("Server seems to be offline, abandoning")
@@ -178,9 +178,9 @@ def __enter__(self):
178178
return self
179179

180180
def __exit__(self, exc_type, exc_value, traceback):
181-
self.shutdown()
181+
self.cleanup()
182182

183-
def shutdown(self):
183+
def cleanup(self):
184184
try:
185185
self._client.setsockopt(zmq.LINGER, 0)
186186
self._client.close()

sqlite_rx/tests/backup/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ def plain_client():
6060
os.kill(server.pid, signal.SIGINT)
6161

6262
server.join()
63-
client.shutdown()
63+
client.cleanup()

sqlite_rx/tests/curezmq/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ def curvezmq_client():
5959
else:
6060
os.kill(server.pid, signal.SIGINT)
6161
server.join()
62-
client.shutdown()
62+
client.cleanup()
6363

sqlite_rx/tests/error/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ def error_client():
1616
client = SQLiteClient(connect_address="tcp://127.0.0.1:5004")
1717
yield client
1818
LOG.info("Shutting down SQLite Client")
19-
client.shutdown()
19+
client.cleanup()

sqlite_rx/tests/plain/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ def plain_client():
3939
else:
4040
os.kill(server.pid, signal.SIGINT)
4141
server.join()
42-
client.shutdown()
42+
client.cleanup()
4343

sqlite_rx/tests/zap/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ def zap_client():
5252
curve_dir=curve_dir,
5353
use_encryption=True)
5454

55-
# server.daemon = True
5655

5756
server.start()
58-
# server.join()
5957
LOG.info("Started Test SQLiteServer")
6058
yield client
6159
if platform.system().lower() == 'windows':
6260
os.system("taskkill /F /pid "+str(server.pid))
6361
else:
6462
os.kill(server.pid, signal.SIGINT)
6563
server.join()
66-
client.shutdown()
64+
client.cleanup()
6765

6866

0 commit comments

Comments
 (0)