简体   繁体   English

使用Python的非阻塞SQL执行

[英]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. 我有一个运行几个独立的SQL语句的Python脚本。 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: 例如,我期望使用psycopg2.cursor执行以下操作:

cursor.execute(sql_statement)
# Run next code block

The recipe is threading . 配方正在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. 如果此行为对您来说不合适,则您可能会注意subprocess模块,该模块能够创建独立运行的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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