简体   繁体   中英

Non-blocking SQL execution using Python

My question might not have a clear answer so please let me know if what I am trying to do is unrealistic.

I have a Python script that runs several independent SQL statements. Due to timeout limits, waiting for the statements to finish execution is not an option. The statements are for maintenance and no output is expected. Is there a way to asynchronously trigger these?

For example, using psycopg2.cursor, I am expecting to do the following:

cursor.execute(sql_statement)
# Run next code block

The recipe is threading . Use it in this way:

from threading import Thread
Thread(target=cursor.execute, args=(sql_statement,)).start()

It's worth noting that your program can not exit properly untill these threads are finished. If this behaviour is improper for you, you may pay attention to the subprocess module, which is capable of creating independently-running tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM