python-3.x - 如果失败,如何退出python子进程
问题描述
我正在使用 SSH bash( command
) 读取远程服务器的一些输出。我需要不断地这样做,所以我产生了子进程。它工作正常,除非服务器服务器在短时间内变得不可用。如果失败,如何重新启动 SSH 命令(整个子进程)?
我有以下代码:
(...)
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
while True:
line = process.stdout.readline()
if lines == "" and process.poll() is not None:
break
(...)
我认为这process.poll() is not None
应该可以解决问题,但它似乎坚持下去
1000 21431 0.0 0.0 0 0 pts/0 Z+ Oct22 0:00 [ssh] <defunct>
并且不脱离while True:
解决方案
我的代码中有愚蠢的拼写错误,if lines == "" and process.poll() is not None:
无法正确执行。另一件事是 ssh_config,设置值以断开@60 秒/1 次尝试是明智的。并避免 process.communicate() 因为它阻塞了整个线程。
推荐阅读
- python - 在 for 循环中检查两个值
- php - 在 Laravel 中创建用户表
- iot - 用于设备的 MQTT 通信,对于 PubSub 数据,RabbitMQ 或 Azure ServiceBus for IotConnect 哪个是更好的选择?
- python - 如何确定 Python 函数采用哪些参数?
- sqlite - 有没有办法让 max 函数忽略 sqlite 列中的空值?
- elasticsearch - 尽管使用了 dfs_query_then_fetch,Elsaticsearch 6.3.1 在云和本地提供了不同的结果。使用python的elasticsearch包查询
- c# - 以编程方式欺骗 MAC ID
- android - 在 Retrofit 中区分 writeTimeout、readTimeout 和 connectTimeout?
- angular - Angular 2+:当搜索是共享组件时在 json 数据中搜索
- json - json - 如何获取 json 到数组