Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

mysql - Python script won't quit because SSHTunnelForwarder hangs

Python:3.8.5 sshtunnel:0.2.1 mysqlclient:1.4.6 mysql-connector:2.2.9

I am using SSHTunnelForwarder to retrieve data from a Mysql database. Here is the script I use to connect via SSH to the DB:

elif self._remote == 1:
  with SSHTunnelForwarder(
    (self._host, 22),
    ssh_password = self._ssh_password,
    ssh_username = self._ssh_login,
    remote_bind_address = (self._remote_bind_address, 3306)) as server:
    print('Connection:',server.local_bind_address)
    cnx = MySQLdb.connect(host = '127.0.0.1',
                          port = server.local_bind_port,
                          user = self._db_user,
                          passwd = self._db_password,
                          db = self._db_name)
    cursor = cnx.cursor()
    res = pd.read_sql(request, con = cnx)
    cursor.close()
    cnx.close()

An example request could be in the following form:

request = 'SELECT * FROM conjunctions AS c LEFT JOIN events AS e ON e.eventId=c.eventId ORDER BY e.eventId;'

The script returns me a valid response, but will not exit to shell. a threading.enumerate() will print this:

[<_MainThread(MainThread, started 139701046208320)>, <paramiko.Transport at 0x74850ac0 (unconnected)>, <paramiko.Transport at 0xae9e4e80 (unconnected)>]

I have found this issue relating to the same problem, however suggested solutions are not working for me. Manually closing the tunnel with a server.stop() does not work. Adding ssh_server.daemon_forward_servers = True as suggested in the issue mentioned above does not work.

Most of all, this problem appears approx 4/5 times the script is launched.

Any help to understand what is going on would be greatly appreciated.

Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...