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