|
5 | 5 | The current operation of a cursor may be cancelled by calling its `.cancel()` method as shown in the example below. |
6 | 6 | """ |
7 | 7 |
|
8 | | -withsql.connect(server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"), |
9 | | -http_path=os.getenv("DATABRICKS_HTTP_PATH"), |
10 | | -access_token=os.getenv("DATABRICKS_TOKEN"))asconnection: |
| 8 | +withsql.connect( |
| 9 | +server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"), |
| 10 | +http_path=os.getenv("DATABRICKS_HTTP_PATH"), |
| 11 | +access_token=os.getenv("DATABRICKS_TOKEN"), |
| 12 | +)asconnection: |
11 | 13 |
|
12 | | -withconnection.cursor()ascursor: |
13 | | -defexecute_really_long_query(): |
14 | | -try: |
15 | | -cursor.execute("SELECT SUM(A.id - B.id) "+ |
16 | | -"FROM range(1000000000) A CROSS JOIN range(100000000) B "+ |
17 | | -"GROUP BY (A.id - B.id)") |
18 | | -exceptsql.exc.RequestError: |
19 | | -print("It looks like this query was cancelled.") |
| 14 | +withconnection.cursor()ascursor: |
20 | 15 |
|
21 | | -exec_thread=threading.Thread(target=execute_really_long_query) |
| 16 | +defexecute_really_long_query(): |
| 17 | +try: |
| 18 | +cursor.execute( |
| 19 | +"SELECT SUM(A.id - B.id) " |
| 20 | ++"FROM range(1000000000) A CROSS JOIN range(100000000) B " |
| 21 | ++"GROUP BY (A.id - B.id)" |
| 22 | + ) |
| 23 | +exceptsql.exc.RequestError: |
| 24 | +print("It looks like this query was cancelled.") |
22 | 25 |
|
23 | | -print("\n Beginning to execute long query") |
24 | | -exec_thread.start() |
| 26 | +exec_thread=threading.Thread(target=execute_really_long_query) |
25 | 27 |
|
26 | | -# Make sure the query has started before cancelling |
27 | | -print("\n Waiting 15 seconds before canceling",end="",flush=True) |
| 28 | +print("\n Beginning to execute long query") |
| 29 | +exec_thread.start() |
28 | 30 |
|
29 | | -seconds_waited=0 |
30 | | -whileseconds_waited<15: |
31 | | -seconds_waited+=1 |
32 | | -print(".",end="",flush=True) |
33 | | -time.sleep(1) |
| 31 | +# Make sure the query has started before cancelling |
| 32 | +print("\n Waiting 15 seconds before canceling",end="",flush=True) |
34 | 33 |
|
35 | | -print("\n Cancelling the cursor's operation. This can take a few seconds.") |
36 | | -cursor.cancel() |
| 34 | +seconds_waited=0 |
| 35 | +whileseconds_waited<15: |
| 36 | +seconds_waited+=1 |
| 37 | +print(".",end="",flush=True) |
| 38 | +time.sleep(1) |
37 | 39 |
|
38 | | -print("\nNow checkingthe cursor status:") |
39 | | -exec_thread.join(5) |
| 40 | +print("\nCancellingthe cursor's operation. This can take a few seconds.") |
| 41 | +cursor.cancel() |
40 | 42 |
|
41 | | -assertnotexec_thread.is_alive() |
42 | | -print("\n The previous command was successfully canceled") |
| 43 | +print("\n Now checking the cursor status:") |
| 44 | +exec_thread.join(5) |
43 | 45 |
|
44 | | -print("\n Now reusing the cursor to run a separate query.") |
| 46 | +assertnotexec_thread.is_alive() |
| 47 | +print("\n The previous command was successfully canceled") |
45 | 48 |
|
46 | | -# We can still execute a new command on the cursor |
47 | | -cursor.execute("SELECT * FROM range(3)") |
| 49 | +print("\n Now reusing the cursor to run a separate query.") |
48 | 50 |
|
49 | | -print("\n Execution was successful. Results appear below:") |
| 51 | +# We can still execute a new command on the cursor |
| 52 | +cursor.execute("SELECT * FROM range(3)") |
50 | 53 |
|
51 | | -print(cursor.fetchall()) |
| 54 | +print("\n Execution was successful. Results appear below:") |
| 55 | + |
| 56 | +print(cursor.fetchall()) |