Skip to content

Commit bd502f8

Browse files
tomcnolantn180005
authored andcommitted
2023-12-05_11:37:53
1 parent 5048624 commit bd502f8

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Program
150150
[BatchInsert.py](https://github.com/Teradata/python-driver/blob/master/samples/BatchInsert.py) | Demonstrates how to insert a batch of rows
151151
[BatchInsertCSV.py](https://github.com/Teradata/python-driver/blob/master/samples/BatchInsertCSV.py) | Demonstrates how to insert a batch of rows from a CSV file
152152
[BatchInsPerf.py](https://github.com/Teradata/python-driver/blob/master/samples/BatchInsPerf.py) | Measures time to insert one million rows
153+
[CancelSleep.py](https://github.com/Teradata/python-driver/blob/master/samples/CancelSleep.py) | Demonstrates how to use the cancel method to interrupt a query
153154
[CharPadding.py](https://github.com/Teradata/python-driver/blob/master/samples/CharPadding.py) | Demonstrates the database's *Character Export Width* behavior
154155
[CommitRollback.py](https://github.com/Teradata/python-driver/blob/master/samples/CommitRollback.py) | Demonstrates commit and rollback methods with auto-commit off.
155156
[DecimalDigits.py](https://github.com/Teradata/python-driver/blob/master/samples/DecimalDigits.py) | Demonstrates how to format decimal.Decimal values.
@@ -171,6 +172,7 @@ Program
171172
[LoadCSVFile.py](https://github.com/Teradata/python-driver/blob/master/samples/LoadCSVFile.py) | Demonstrates how to load data from a CSV file into a table
172173
[LobLocators.py](https://github.com/Teradata/python-driver/blob/master/samples/LobLocators.py) | Demonstrates how to use LOB locators
173174
[MetadataFromPrepare.py](https://github.com/Teradata/python-driver/blob/master/samples/MetadataFromPrepare.py) | Demonstrates how to prepare a SQL request and obtain SQL statement metadata
175+
[MultiThread.py](https://github.com/Teradata/python-driver/blob/master/samples/MultiThread.py) | Demonstrates how to use multiple threads to load data in parallel
174176
[ParamDataTypes.py](https://github.com/Teradata/python-driver/blob/master/samples/ParamDataTypes.py) | Demonstrates how to specify data types for parameter marker bind values
175177
[ShowCommand.py](https://github.com/Teradata/python-driver/blob/master/samples/ShowCommand.py) | Displays the results from the `SHOW` command
176178
[StoredProc.py](https://github.com/Teradata/python-driver/blob/master/samples/StoredProc.py) | Demonstrates how to create and call a SQL stored procedure

samples/CancelSleep.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2023 by Teradata Corporation. All rights reserved.
2+
3+
# This sample program demonstrates how to use the cancel method to interrupt a query
4+
# executing in another thread.
5+
6+
import teradatasql
7+
import threading
8+
import time
9+
10+
def WorkerThread (cur):
11+
12+
t = threading.current_thread ()
13+
14+
sql = "select mysleep (10)" # sleep for 10 seconds
15+
print ("Worker thread", t.ident, sql)
16+
try:
17+
cur.execute (sql)
18+
except Exception as ex:
19+
print ("Worker thread", t.ident, str (ex).split ("\n") [0])
20+
21+
# end WorkerThread
22+
23+
with teradatasql.connect (host="whomooz", user="guest", password="please") as con:
24+
with con.cursor () as cur:
25+
26+
tMain = threading.current_thread ()
27+
28+
sql = "drop function mysleep"
29+
print ("Main thread", tMain.ident, sql)
30+
cur.execute (sql, ignoreErrors=5589)
31+
32+
sql = "create function mysleep (integer) returns integer language c no sql parameter style sql external name 'CS!udfsleep!udfsleep.c!F!udfsleep'"
33+
print ("Main thread", tMain.ident, sql)
34+
cur.execute (sql)
35+
try:
36+
tWorker = threading.Thread (target=WorkerThread, args=(cur,))
37+
print ("Main thread", tMain.ident, "starting worker thread")
38+
tWorker.start ()
39+
40+
print ("Main thread", tMain.ident, "sleeping for 5 seconds")
41+
time.sleep (5)
42+
43+
print ("Main thread", tMain.ident, "calling cancel")
44+
con.cancel ()
45+
print ("Main thread", tMain.ident, "completed cancel")
46+
47+
print ("Main thread", tMain.ident, "waiting for worker thread", tWorker.ident, "to finish")
48+
tWorker.join ()
49+
print ("Main thread", tMain.ident, "done waiting for worker thread", tWorker.ident)
50+
51+
finally:
52+
sql = "drop function mysleep"
53+
print ("Main thread", tMain.ident, sql)
54+
cur.execute (sql)

samples/MultiThread.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2023 by Teradata Corporation. All rights reserved.
2+
3+
# This sample program demonstrates how to use multiple threads to load data in parallel.
4+
5+
import teradatasql
6+
import threading
7+
import time
8+
9+
def WorkerThread (con, sTableName, aaoData):
10+
11+
t = threading.current_thread ()
12+
13+
# Connection objects are thread safe. Threads can share a connection.
14+
# Cursor objects are not thread safe. Each thread needs its own cursor.
15+
16+
with con.cursor () as cur:
17+
18+
sql = "create volatile table " + sTableName + " (c1 integer, c2 varchar(100)) on commit preserve rows"
19+
print ("Worker thread", t.ident, sql)
20+
cur.execute (sql)
21+
22+
sql = "insert into " + sTableName + " values (?, ?)"
23+
print ("Worker thread", t.ident, sql)
24+
cur.execute (sql, aaoData)
25+
26+
# end WorkerThread
27+
28+
with teradatasql.connect (host="whomooz", user="guest", password="please") as con:
29+
30+
tMain = threading.current_thread ()
31+
32+
tWorker1 = threading.Thread (target=WorkerThread, args=(con, "voltab1", [
33+
[1, "abc"],
34+
[2, "def"],
35+
[3, "ghi"],
36+
]))
37+
print ("Main thread", tMain.ident, "starting worker thread #1")
38+
tWorker1.start ()
39+
40+
tWorker2 = threading.Thread (target=WorkerThread, args=(con, "voltab2", [
41+
[10, "rst"],
42+
[20, "uvw"],
43+
[30, "xyz"],
44+
]))
45+
print ("Main thread", tMain.ident, "starting worker thread #2")
46+
tWorker2.start ()
47+
48+
print ("Main thread", tMain.ident, "waiting for worker thread", tWorker1.ident, "to finish")
49+
tWorker1.join ()
50+
print ("Main thread", tMain.ident, "done waiting for worker thread", tWorker1.ident)
51+
52+
print ("Main thread", tMain.ident, "waiting for worker thread", tWorker2.ident, "to finish")
53+
tWorker2.join ()
54+
print ("Main thread", tMain.ident, "done waiting for worker thread", tWorker2.ident)
55+
56+
with con.cursor () as cur:
57+
58+
sql = "select * from voltab1 order by 1 ; select * from voltab2 order by 1"
59+
print ("Main thread", tMain.ident, sql)
60+
cur.execute (sql)
61+
[ print (row) for row in cur.fetchall () ]
62+
cur.nextset ()
63+
[ print (row) for row in cur.fetchall () ]

samples/udfsleep.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Copyright 2023 by Teradata Corporation. All rights reserved. */
2+
#include <unistd.h>
3+
4+
void udfsleep (int *seconds, int *result, char exception[6])
5+
{
6+
sleep ((unsigned int) *seconds) ;
7+
*result = 1 ;
8+
}

0 commit comments

Comments
 (0)