for some reason the connection timeout does not seem to work when connecting to a Firebird database. For example, if an incorrect IP address is specified, the script hangs and waits for a long time.
#!/usr/bin/python3
import fdb
import timeout
HOST = '172.19.76.10'
DBS = [
'test_db_1102',
'test_db_10025',
'test_catalog',
]
USER = 'SYSDBA'
PASSWORD = '123'
def check_db(DB):
print(DB)
con = fdb.connect(host = HOST, database = DB, user = USER, password = PASSWORD )
cur = con.cursor()
cur.execute('select first(1) * from objects')
print(cur.fetchall())
for DB in DBS:
timeout.timeout_check(check_db(DB),5)
list timeout.py:
#!/usr/bin/python3
import multiprocessing
import time
def timeout_check(func, func_timeout):
p = multiprocessing.Process(target=func)
p.start()
p.join(func_timeout)
if p.is_alive():
print ("running... let's kill it...")
p.terminate()
p.join()
I tried using other timeout functions based on the signal library, but that also does not work
1 Answer 1
Your timeout_check function accepts a function as a first argument, yet you're passing in the results of a function call check_db(DB), i.e. this function is called immediately.
You probably want to send a partially applied function to timeout_check:
from functools import partial
...
for DB in DBS:
timeout.timeout_check(func=partial(check_db, DB), func_timeout=5)
1 Comment
import timeoutExplore related questions
See similar questions with these tags.
import timeoutis importing ? Apart the answer (I was writing the same thing in a comment and then cancel it) you can also look at Timeout on a function call if needed