データベース照会には2つの機能があります。別の2つのクエリを仮定して、これらを並列に実行して同じデータベースをクエリする方法と、残りのコードの実行を続ける前に両方の結果が返るのを待つ方法はありますか?ここでpythonを使用してデータベースを並列に照会する方法
def query1(param1, param2):
result = None
logging.info("Connecting to database...")
try:
conn = connect(host=host, port=port, database=db)
curs = conn.cursor()
curs.execute(query)
result = curs
curs.close()
conn.close()
except Exception as e:
logging.error("Unable to access database %s" % str(e))
return result
def query2(param1, param2):
result = None
logging.info("Connecting to database...")
try:
conn = connect(host=host, port=port, database=db)
curs = conn.cursor()
curs.execute(query)
result = curs
curs.close()
conn.close()
except Exception as e:
logging.error("Unable to access database %s" % str(e))
return result
私はあなたが[ 'threading'](httpsに見てみることができますね://ドキュメント.python.org/3/library/threading.html)ライブラリを標準コレクションから削除します。ここでは、pythonスレッドを使用する方法(および戻り値を収集する方法)について説明しています。http://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread- in-python(これは実際にはPython側での並列処理ではありませんが、両方の要求はほぼ同時に行われます)。 – mgc