piskvorky / sqlitedict

Persistent dict, backed by sqlite3 and pickle, multithread-safe.
Apache License 2.0
1.17k stars 131 forks source link

autocommit does not commit if program exits #93

Closed endlisnis closed 4 years ago

endlisnis commented 5 years ago

I ran into a problem in one of my programs where it was not committing some changes because my script exited shortly after the updates.

Here is a example script that reliably reproduces the effect (for me):

import json
import sqlitedict
import time

class Dict(sqlitedict.SqliteDict):
    def __init__(self, name):
        sqlitedict.SqliteDict.__init__(
            self,
            f'{name}.sqlite', autocommit=True,
            encode=json.dumps, decode=json.loads )

d = Dict('d')
now = int(time.time())

for i in range(100):
    d[i] = now

If I follow that up with a select, I only see 18 entries (sometimes I see zero entries):

$ sqlite3 d.sqlite 'SELECT * from unnamed'
0|1553346705
1|1553346705
2|1553346705
3|1553346705
4|1553346705
5|1553346705
6|1553346705
7|1553346705
8|1553346705
9|1553346705
10|1553346705
11|1553346705
12|1553346705
13|1553346705
14|1553346705
15|1553346705
16|1553346705
17|1553346705
18|1553346705
piskvorky commented 5 years ago

You're right. If the program exits, all internal threads / queues are discarded by Python/OS, at indeterminate time.

I'm not sure what we could do about it on our end… @mpenkov maybe make some operations (like commit) blocking, instead of using background threads and queues?

endlisnis commented 5 years ago

I thought commit was blocking already. This comment implies it's blocking: https://github.com/RaRe-Technologies/sqlitedict/issues/52#issuecomment-336732757

piskvorky commented 5 years ago

A commit should definitely be blocking IMO. If true, I consider this a bug. Thanks for reporting.

I'm not sure when I'll get to fixing this -- please feel free to suggest a PR.

endlisnis commented 5 years ago

"commit" is blocking, but "autocommit" is not. I think this would be as simple as calling "commit" as part of setitem, if "autocommit" is True.

piskvorky commented 5 years ago

OK, go for it! PR welcome.

endlisnis commented 5 years ago

My PR fixes the problem for me. Is there anything more I need to do?

endlisnis commented 5 years ago

Is there anything more I can do to assist in getting this fix merged?

endlisnis commented 4 years ago

I see this issue is closed, but the most recent released version of this project is from 2018. Are there any plans on doing another release soon? It would be nice to be able to use auto-commit from the official released version.

piskvorky commented 4 years ago

@mpenkov WDYT?

mpenkov commented 4 years ago

Yes, I will do a new release tomorrow.

mpenkov commented 4 years ago

Forgot to post the URL to the new version here: https://pypi.org/project/sqlitedict/1.7.0/

endlisnis commented 4 years ago

Forgot to post the URL to the new version here: https://pypi.org/project/sqlitedict/1.7.0/

Great. That seems to work (including with pip), so that satisfies my technical requirements, but it is NOT listed as the "latest release" on github. 1.6.0 is still listed as the "latest" release.

mpenkov commented 4 years ago

Thank you for pointing that out. Should be fixed now: https://github.com/RaRe-Technologies/sqlitedict/releases/tag/1.7.0