1

I've just created an script with a long time query, and I added a signal handled that when I press CTRL+C, it launches a connection.close().

But when I run the py script and press CTRL+C it just waits until the execute has ended to handle the signal.

Is there any way I can cancel it while running?

def signal_term_handler(signal, frame):
    connection.cancel()
    sys.exit('Excution stopped manually.')

ip = IP
port = PORT
SID = SID
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
connection = cx_Oracle.connect(USER, PASS, dsn_tns)
cursor = connection.cursor()


signal.signal(signal.SIGTERM, signal_term_handler)
signal.signal(signal.SIGINT, signal_term_handler)
cursor.execute(QUERY)

QUERY is a query with multiple selects that lasts for around 4 minutes.

Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
Sergio González
  • 142
  • 2
  • 12
  • What are the client & DB server platforms? What Oracle versions? Are you using a VM? Is your firewall blocking/inlining out of band breaks? Tried a sqlnet.ora DISABLE_OOB=on parameter? – Christopher Jones Apr 26 '18 at 22:34
  • Thanks 2 @ChristopherJones. Im using oracle-xe-11 on docker, and the last version of cx_oracle. Where shall I use the slqnet.ora DISABLE_OOB? I've checked the dock and it might be off (https://docs.oracle.com/cd/B28359_01/network.111/b28317/sqlnet.htm#NETRF185) – Sergio González Apr 27 '18 at 06:36
  • When I test your code and leave out the signal handlers stuff, the connection is cleaned up right after hitting ctrl-C. Why not use regular python exceptions in a try block? –  Aug 24 '18 at 12:08
  • @SergioGonzález here is some info on sqlnet.ora files: https://oracle.github.io/node-oracledb/doc/api.html#tnsadmin Although the documentation is for node-oracledb, it applies to cx_Oracle (and many other Oracle client applications) – Christopher Jones Sep 05 '18 at 01:19

1 Answers1

1
cursor = connection.cursor()

try:
  cursor.execute(QUERY)
except KeyboardInterrupt:
  sys.exit('Excution stopped manually.')

This should be close to what you want. It looks like cx_Oracle re-raises the KeyBoardInterrupt so to do a complete catch you need to nest the handler in an extra outer try: except KeyboardInterrupt: