From 37e449d62c147e2eeb36d1dca1949695b0afb241 Mon Sep 17 00:00:00 2001 From: Anthony Tuininga Date: Mon, 20 Mar 2017 13:20:01 -0600 Subject: [PATCH] Add sample demonstrating the use of threads and concurrent execution of database operations. --- samples/Threads.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 samples/Threads.py diff --git a/samples/Threads.py b/samples/Threads.py new file mode 100644 index 0000000..6cfbb82 --- /dev/null +++ b/samples/Threads.py @@ -0,0 +1,65 @@ +#------------------------------------------------------------------------------ +# Copyright 2017, Oracle and/or its affiliates. All rights reserved. +#------------------------------------------------------------------------------ + +#------------------------------------------------------------------------------ +# Threads.py +# This script demonstrates the use of threads with cx_Oracle. A session pool +# is used so that multiple connections are available to perform work on the +# database. Only one operation (such as an execute or fetch) can take place at +# a time on a connection. In the below example, one of the threads performs +# dbms_lock.sleep while the other performs a query. +# +# This script requires cx_Oracle 2.5 and higher. +#------------------------------------------------------------------------------ + +from __future__ import print_function + +import cx_Oracle +import threading + +pool = cx_Oracle.SessionPool("cx_Oracle", "dev", "localhost/orcl", 2, 5, 1, + threaded = True) + +def TheLongQuery(): + conn = pool.acquire() + cursor = conn.cursor() + cursor.arraysize = 25000 + print("TheLongQuery(): beginning execute...") + cursor.execute(""" + select * + from + TestNumbers + cross join TestNumbers + cross join TestNumbers + cross join TestNumbers + cross join TestNumbers + cross join TestNumbers""") + print("TheLongQuery(): done execute...") + while True: + rows = cursor.fetchmany() + if not rows: + break + print("TheLongQuery(): fetched", len(rows), "rows...") + print("TheLongQuery(): all done!") + + +def DoALock(): + conn = pool.acquire() + cursor = conn.cursor() + print("DoALock(): beginning execute...") + cursor.callproc("dbms_lock.sleep", (5,)) + print("DoALock(): done execute...") + + +thread1 = threading.Thread(None, TheLongQuery) +thread1.start() + +thread2 = threading.Thread(None, DoALock) +thread2.start() + +thread1.join() +thread2.join() + +print("All done!") +