-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathquery_cancel.py
56 lines (41 loc) · 1.8 KB
/
query_cancel.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
55
56
from databricks import sql
import os, threading, time
"""
The current operation of a cursor may be cancelled by calling its `.cancel()` method as shown in the example below.
"""
with sql.connect(
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
access_token=os.getenv("DATABRICKS_TOKEN"),
) as connection:
with connection.cursor() as cursor:
def execute_really_long_query():
try:
cursor.execute(
"SELECT SUM(A.id - B.id) "
+ "FROM range(1000000000) A CROSS JOIN range(100000000) B "
+ "GROUP BY (A.id - B.id)"
)
except sql.exc.RequestError:
print("It looks like this query was cancelled.")
exec_thread = threading.Thread(target=execute_really_long_query)
print("\n Beginning to execute long query")
exec_thread.start()
# Make sure the query has started before cancelling
print("\n Waiting 15 seconds before canceling", end="", flush=True)
seconds_waited = 0
while seconds_waited < 15:
seconds_waited += 1
print(".", end="", flush=True)
time.sleep(1)
print("\n Cancelling the cursor's operation. This can take a few seconds.")
cursor.cancel()
print("\n Now checking the cursor status:")
exec_thread.join(5)
assert not exec_thread.is_alive()
print("\n The previous command was successfully canceled")
print("\n Now reusing the cursor to run a separate query.")
# We can still execute a new command on the cursor
cursor.execute("SELECT * FROM range(3)")
print("\n Execution was successful. Results appear below:")
print(cursor.fetchall())