为什么我无法通过 Fabric 与 redis-cli 交互?
我有一个像这样设置的结构任务:
@task
def cli():
command = [
os.path.join(env.servers_path, "bin", "redis-cli"),
]
run(" ".join(command))
运行它会给我一个提示,但没有交互性:
$ fab cli
[server] Executing task 'cli'
[server] Executing task 'redis.cli'
[server] run: /path/to/bin/redis-cli
[server] out: redis 127.0.0.1:6379> help
<no output produced>
进一步键入会产生一个“out:”提示,我在该提示上键入,但在任何时候我都不会从redis返回任何信息。
但是,如果我更改为其他交互提示,我确实会得到交互性:
@task
def cli():
command = [
"python"
]
run(" ".join(command))
产生:
$ fab cli
[server] Executing task 'cli'
[server] run: python
[server] out: Python 2.4.3 (#1, Sep 3 2009, 15:37:37)
[server] out: [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
[server] out: Type "help", "copyright", "credits" or "license" for more information.
[server] out: >>> a = 1
[server] out: >>> a
[server] out: 1
[server] out: >>>
任何人都可以向我提供任何关于为什么 redis-cli 表现不佳的提示吗?我想打开该项目的错误,但我想首先更好地理解它。
I have a fabric task set up like this:
@task
def cli():
command = [
os.path.join(env.servers_path, "bin", "redis-cli"),
]
run(" ".join(command))
Running it gives me a prompt, but no interactivity:
$ fab cli
[server] Executing task 'cli'
[server] Executing task 'redis.cli'
[server] run: /path/to/bin/redis-cli
[server] out: redis 127.0.0.1:6379> help
<no output produced>
Typing further produces an "out: " prompt that I type onto, but at no point do I get anything back from redis.
However, if I change to some other interactive prompt, I do get interactivity:
@task
def cli():
command = [
"python"
]
run(" ".join(command))
produces:
$ fab cli
[server] Executing task 'cli'
[server] run: python
[server] out: Python 2.4.3 (#1, Sep 3 2009, 15:37:37)
[server] out: [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
[server] out: Type "help", "copyright", "credits" or "license" for more information.
[server] out: >>> a = 1
[server] out: >>> a
[server] out: 1
[server] out: >>>
Can anyone offer me any hints as to why redis-cli doesn't behave nicely? I'd like to open a bug with that project, but I'd like to understand it better first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是因为交互模式下的 redis-cli 实际上是为与终端一起工作而设计的,而 Fabric 可能运行 redis-cli 重定向标准输入/输出文件描述符。
例如,以下命令可以正常工作:
而以下命令则不能:
redis-cli 和提供类似 readline 功能的 linenoise 库无法正确刷新带有非终端文件描述符的输出。我没有尝试过使用 Fabric 的一个可能的解决方法是停用 linenoise:
通过将 TERM 变量定义为哑,linenoise 默认在一个非常基本的代码路径上,该路径恰好使用一个简单的 printf 来处理提示并在之后刷新输出它的显示。如果您可以在流程环境中设置此变量,它可能会解决您的结构问题。
I think this is due to the fact redis-cli in interactive mode is really designed to work with a terminal, while fabric probably runs redis-cli redirecting standard input/output file descriptors.
For instance, the following command works fine:
while the following one does not:
redis-cli and the linenoise library providing readline-like facilities do not flush correctly the output with a non terminal file descriptor. A possible workaround which I have not tried with fabric, is to deactivate linenoise:
By defining the TERM variable to dumb, linenoise defaults on a very basic code path which happens to use a simple printf to deal with the prompt and flush the output just after its display. It may solve your issue with fabric provided you can set this variable in the process environment.
我刚刚找到了一个简单而酷的答案;通过 echo 和管道发送您想要的任何命令:
echo "keys *" | redis-cli
run("echo 'keys *' | redis-cli")
I just found a simple and cool answer; send whatever command you want through echo and pipe:
echo "keys *" | redis-cli
run("echo 'keys *' | redis-cli")