我的要求是从调试器和pycharm提示符中获取调试数据:
假设我有一个旧式的 python 同步程序,但有一个错误:
def listdir(self, remote_path):
with ssh.connect(self.connection_settings['host'], username=self.connection_settings['username'],
password=self.connection_settings['password']) as conn:
with conn.start_sftp_client() as sftp:
sftp.chdir(remote_path) # breakpoint here
files = sftp.readdir()
files_name = [file.filename for file in files]
return files_name
我会停止它,转到调试器,单击调试器控制台->显示 python 提示符 然后在运行时调试我的问题:
> sftp.getcwd()
bad_user_location
> sftp.chdir(good_base_location)
> sftp.readdir()
只需即时重写代码并测试状态和运行时。 (这一直是我调试的方式,在 Microsoft 语言开发中使用
immediate window
,在 python 和其他脚本语言中使用 REPL。
现在我有一个异步程序:
async def listdir(self, remote_path):
async with asyncssh.connect(self.connection_settings['host'], username=self.connection_settings['username'],
password=self.connection_settings['password']) as conn:
async with conn.start_sftp_client() as sftp:
await sftp.chdir(remote_path) # breakpoint here
files = await sftp.readdir()
files_name = [file.filename for file in files]
return files_name
我停止它,转到调试器,单击调试器控制台->显示 python 提示符 然后在运行时调试我的问题:
> sftp.getcwd()
<CoroWrapper SFTPClient.getcwd() running at....truncated info
nest_asyncio.events.new_event_loop().create_task(sftp.getcwd())
但它只是将其添加到循环中,而不运行它。 <Task pending coro=<SFTPClient.getcwd() running at...
>d.run_sync(sftp.getcwd())
directory_name
>d.run_sync(sft.chdir(somewhere_else))
0 # exit code
我在频道 v2.0 中在 Andrew Gadwins 伟大的文章
下发现了类似的内容 >asgiref.sync.async_to_sync(sftp.getcwd())
><asgiref.sync.AsyncToSync object at 0x7ff94c7b37e0> ... created at <input>:1> was never yielded from
>ERROR:asyncio:<CoroWrapper SFTPClient.getcwd()
我认为能够在运行时调试协程至关重要,因此我非常重视您的解决方案。
谢谢