gpt4 book ai didi

python-3.x - 连接到远程数据库脚本后不退出

转载 作者:行者123 更新时间:2023-12-03 15:36:17 33 4
gpt4 key购买 nike

我连接到远程数据库。为此,我正在使用 sshtunnel。我没有问题连接到数据库并访问数据。

我的问题是,我的脚本在所有操作之后都没有退出。
我创建连接、下载数据、打印数据、停止 ssh 连接、打印“退出”。该脚本在 server.stop() 行停止工作并且不打印“停止”。我需要中断它以停止运行此代码。

这是代码:

from sshtunnel import SSHTunnelForwarder
from sqlalchemy import create_engine
import pandas as pd


server = SSHTunnelForwarder(
('host', 22),
ssh_password='password',
ssh_username='username',
remote_bind_address=('127.0.0.1', 3306)
)
server.start()

engine = create_engine(
'mysql+mysqldb://db_user:db_pass@127.0.0.1:{}/temps'.format(server.local_bind_port))

query = 'SELECT * FROM temp'

df = pd.read_sql(query, engine)
print(df.head())
print(df.tail())

server.stop()
print('stop')

此脚本不打印“停止”。

问题:为什么这段代码不能停止工作?

编辑:

我加了

trace_logger = create_logger(loglevel="TRACE")



在此之后,我注意到一些有趣的事情。带有数据传输的代码不包含一行:传输已关闭。我在没有发送 sql 查询的情况下检查了我的代码,脚本已正确完成。

带数据传输的日志
2018-10-07 18:41:43,274| WAR | MainThrea/0967@sshtunnel | Could not read SSH configuration file: ~/.ssh/config
2018-10-07 18:41:43,275| INF | MainThrea/0993@sshtunnel | 0 keys loaded from agent
2018-10-07 18:41:43,275| INF | MainThrea/1042@sshtunnel | 0 keys loaded from host directory
2018-10-07 18:41:43,275| INF | MainThrea/0914@sshtunnel | Connecting to gateway: 192.168.0.102:22 as user ‘xxx’
2018-10-07 18:41:43,275| DEB | MainThrea/0917@sshtunnel | Concurrent connections allowed: True
2018-10-07 18:41:43,275| DEB | MainThrea/1369@sshtunnel | Trying to log in with password: xxx
2018-10-07 18:41:43,600| INF | Srv-56620/1389@sshtunnel | Opening tunnel: 0.0.0.0:56620 <> 127.0.0.1:3306
….. # data transfer
2018-10-07 18:41:43,945| INF | MainThrea/1328@sshtunnel | Closing all open connections...
<Logger sshtunnel.SSHTunnelForwarder (TRACE)>
2018-10-07 18:41:43,945| DEB | MainThrea/1332@sshtunnel | Listening tunnels: 0.0.0.0:56620
2018-10-07 18:41:43,945| INF | MainThrea/1408@sshtunnel | Shutting down tunnel ('0.0.0.0', 56620)
2018-10-07 18:41:44,048| INF | Srv-56620/1395@sshtunnel | Tunnel: 0.0.0.0:56620 <> 127.0.0.1:3306 released

没有数据传输的日志
2018-10-07 18:37:54,016| WAR | MainThrea/0967@sshtunnel | Could not read SSH configuration file: ~/.ssh/config
2018-10-07 18:37:54,017| INF | MainThrea/0993@sshtunnel | 0 keys loaded from agent
2018-10-07 18:37:54,017| INF | MainThrea/1042@sshtunnel | 0 keys loaded from host directory
2018-10-07 18:37:54,017| INF | MainThrea/0914@sshtunnel | Connecting to gateway: 192.168.0.102:22 as user ‘xxx'
2018-10-07 18:37:54,017| DEB | MainThrea/0917@sshtunnel | Concurrent connections allowed: True
2018-10-07 18:37:54,017| DEB | MainThrea/1369@sshtunnel | Trying to log in with password: xxx
2018-10-07 18:37:54,342| INF | Srv-56560/1389@sshtunnel | Opening tunnel: 0.0.0.0:56560 <> 127.0.0.1:3306
2018-10-07 18:37:54,363| INF | MainThrea/1328@sshtunnel | Closing all open connections...
<Logger sshtunnel.SSHTunnelForwarder (TRACE)>
2018-10-07 18:37:54,363| DEB | MainThrea/1332@sshtunnel | Listening tunnels: 0.0.0.0:56560
2018-10-07 18:37:54,363| INF | MainThrea/1408@sshtunnel | Shutting down tunnel ('0.0.0.0', 56560)
2018-10-07 18:37:54,448| INF | Srv-56560/1395@sshtunnel | Tunnel: 0.0.0.0:56560 <> 127.0.0.1:3306 released
2018-10-07 18:37:54,448| DEB | MainThrea/1422@sshtunnel | Transport is closed

最佳答案

日志检查后发现 sqlalchemy打开连接是问题所在。

我们创建了 trace_logger = sshtunnel.create_logger(loglevel="TRACE")并将其传递给 SSHTunnelForwarder
给任何人以供将来引用:

添加 engine.dispose()之后 df.read_sql将关闭所有挂起的数据库连接,允许关闭 ssh 隧道。

Relevant documentation from sqlalchemy

关于python-3.x - 连接到远程数据库脚本后不退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52612080/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com