apsw waiting for another write to finish

35 views
Skip to first unread message

Peng Yu

unread,
Mar 10, 2020, 2:53:43 PM3/10/20
to python...@googlegroups.com
Hi,

I have the following python code to write something to a sqlar file.

import apsw
conn = apsw.Connection(sys.argv[1])
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS sqlar(
name TEXT PRIMARY KEY
, mode INT
, mtime INT
, sz INT
, data BLOB)
''')

import zlib
data = sys.stdin.buffer.read()
c.execute('REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)', [sys.argv[2], 0,
0, len(data), zlib.compress(data)])

But the problem is when multiple processes are trying to write to the
same database file, I got the following error. What is the correct way
to change the code so that it will not raise an exception but write to
the same database when other write operations are finished?

File "xxx.py", line 28, in <module>
c.execute('REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)',
[sys.argv[2], 0, 0, len(data), zlib.compress(data)])
File "src/cursor.c", line 236, in resetcursor
apsw.BusyError: BusyError: database is locked

--
Regards,
Peng

Roger Binns

unread,
Mar 10, 2020, 3:00:22 PM3/10/20
to python...@googlegroups.com
On 3/9/20 5:25 PM, Peng Yu wrote:
> apsw.BusyError: BusyError: database is locked

https://rogerbinns.github.io/apsw/tips.html#busy-handling

Set a busy timeout to what seems like a reasonably large number to you,
and all should go well.

If you are doing things like leaving transactions open (only one can be
in progress at a time), then good practise is to alter the code to have
the transaction open for less time. 'with' works well:

https://rogerbinns.github.io/apsw/connection.html?highlight=transaction#apsw.Connection.__enter__

Roger


signature.asc

Peng Yu

unread,
Mar 11, 2020, 10:54:32 AM3/11/20
to python...@googlegroups.com
Hi Roger, Thanks. Could you show me the modified code? I am not sure I
can get it completely right. Thanks.
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "python-sqlite" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to python-sqlit...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python-sqlite/1b41020f-e439-3c7d-ad28-f1abd46ac7bc%40rogerbinns.com.
>


--
Regards,
Peng
Reply all
Reply to author
Forward
Message has been deleted
0 new messages