-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exiting with ctrl-c #74
Comments
Here is another example: conn = await aiosqlite.connect('db.sqlite') During the execution of q, ctrl-c stops the query but the script does not exit. ctrl-c again stops the script with the following message:
Surrounding cursor.execute with try-except did not catch ctrl-c during the execution of q. |
The simplest solution may be to make the thread that implements a DB connection a daemon thread. Ctrl-C interrupts the main thread, but the process won't terminate until all (non-daemon) threads have completed. It's harder if you need to tidy up the connection thread on Ctrl-C, but given that the program is exiting anyway, this might not be necessary. |
Thanks. We ended up with just making sure all db connections get to be closed. As long as there was no open connection, the program exited cleanly. |
That doesn't help in cases where (for whatever reason) the application doesn't manage to close all connections. I would prefer it if the issue was handled in the library itself. Can this issue be reopened please? |
Oh, sure. |
I have a working example of CTRL-C, but it requires #89 and #90 . I'll show some code then explain what it does, and discuss why it's not so simple for the library to handle everything internally. import asyncio, aiorun, aiosqlite
async def conn(db):
sql = ''' select sf.name, dd.value, count(*)
from document_data dd inner join structured_field sf on dd.field_id = sf.id
group by sf.name, dd.value
limit 10 '''
try:
print('running query...')
async with db.execute(sql) as cursor:
async for row in cursor:
print('<row>')
print('query completed')
except asyncio.CancelledError: # <1>
print('interrupting the DB connection')
await db.interrupt()
print('leaving conn')
async def main():
dbname = 'storage.db'
try:
async with aiosqlite.connect(dbname) as db:
print('connected')
await conn(db)
except aiosqlite.OperationalError:
print('query was cancelled')
else:
asyncio.get_running_loop().stop()
if __name__ == '__main__':
aiorun.run(main()) My sqlite DB
This is output if you press CTRL-C while the query is running:
I guess what I'm trying to say is, I'm not sure whether handling the interrupt should be moved into the internals of aiosqlite or not. I think I would prefer not, but even though I'm a heavy user of sqlite I've never had to use the interrupt API, so I'm not sure. The code example above was run on Python 3.8.5. But again note that it does depends on #89 and #90 to be able to handle the consequences of |
Thanks. The way the example code handles the CTRL-C would be perfect for my use case. |
@rkiminsilico are you running on Windows or Linux? (or mac?) |
The package (open-cravat) is run on all three platforms (Windows, Mac, and Linux). |
Oops, yes I see you mentioned windows in the initial report. aiorun should work on Windows too, with respect to CTRL-C, but it hasn't been used as much on windows (as linux). |
Normally, the Connection thread prevents program exit unless close() is explicitly closed, unless `deaemonic=True` is passed. It defaults to `False` which means aiosqlite's behaviour remains unaffected. This PR would also address issues which have been opened for some time now: omnilib#74 omnilib#299
Description
For example, if a script
is interrupted with ctrl-c and it does not exit to the console. If conn and cursor are closed before ctrl-c or inside try-except catching ctrl-c, it does.
However, it can be tricky with multiple cursors and db connections in different parts of a program, to know and close them properly when interrupted with ctrl-c.
What would be the best way to handle KeyboardInterrupt and aiosqlite? Or, would it be possible that aiosqlite does not hang when interrupted before closing connections? aiosqlite3 does not show this behavior.
Details
The text was updated successfully, but these errors were encountered: