我正在尝试使用gevent 获取多线程解决方案来执行一系列任务,其中一些任务使用 fabric api 。尝试monkeypatch时,结构无法读取env并提示输入密码。当没有修补一切正常时,但执行似乎不是异步的。我为这个问题创建了以下玩具示例:
from gevent import monkey, spawn, sleep, joinall
monkey.patch_socket() # If removed, "appears to" runs serial
from fabric.api import run, env, settings
import os
env.use_ssh_config = True
env.host_string = "user@remotehost"
env.user = "user"
env.key_filename = os.path.expanduser('~/.ssh/id_rsa_4096')
env.password = ""
env.port = 22
def toyTask(char):
command = "ls %s*" %char
print command
with settings(warn_only=True):
run(command)
sleep()
charList = list(map(chr,range(97,123))) # Creates ['a','b',...........'z']
threads = [spawn(toyTask, char) for char in charList]
joinall(threads)
以其当前形式,返回:
me @ localhost:〜$ python gevent_fab.py
是的a *
[user @ remotehost]运行:ls a *
ls b *
[user @ remotehost]运行:ls b *
ls c *..
..
[user @ remotehost]运行:ls z *
ls z *无法找到记录器“paramiko.transport”的处理程序 [user @ remotehost] '用户'的登录密码:
如果删除monkey.patch_socket
,则Fabric不再提示输入密码并返回预期输出,如下所示:
是a *
[user @ remotehost]运行:ls a *
[user @ remotehost] out:kegg.csh kegg.pl
[user @ remotehost] out:ls b *
[user @ remotehost]运行:ls b *
[user @ remotehost] out:bin:
[user @ remotehost] out:afiedt.buf hash_mod.py-orig renamePat.plls c *
..
...
ls z *
[user @ remotehost]运行:ls z *
[user @ remotehost] out:ls:无法访问z *:没有这样的文件或目录
[user @ remotehost] out
monkey.patch_socket()
?