为什么我无法通过 Fabric 与 redis-cli 交互?

发布于 2024-12-27 01:50:15 字数 1185 浏览 2 评论 0原文

我有一个像这样设置的结构任务:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2025-01-03 01:50:15

我认为这是因为交互模式下的 redis-cli 实际上是为与终端一起工作而设计的,而 Fabric 可能运行 redis-cli 重定向标准输入/输出文件描述符。

例如,以下命令可以正常工作:

python | cat

而以下命令则不能:

redis-cli | cat

redis-cli 和提供类似 readline 功能的 linenoise 库无法正确刷新带有非终端文件描述符的输出。我没有尝试过使用 Fabric 的一个可能的解决方法是停用 linenoise:

TERM=dumb redis-cli | cat

通过将 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:

python | cat

while the following one does not:

redis-cli | cat

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:

TERM=dumb redis-cli | cat

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.

乖乖兔^ω^ 2025-01-03 01:50:15

我刚刚找到了一个简单而酷的答案;通过 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
  • or specifically for your case: run("echo 'keys *' | redis-cli")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文