python - 正确理解 pexpect 的异步

标签 python python-3.x asynchronous pexpect

我正在尝试使用 pexpect 编写一个 gdb 前端来与 gdb/mi 进行通信。我是 pexpect 的新手,并试图找出 Expect_exact() 函数的异步属性。我编写了一个简单的测试,如下所示:

def attach(str):
    global p
    p=pexpect.spawnu('sudo gdb --interpreter=mi')
    p.expect_exact("(gdb) ")
    GDB_Engine.send_command("set target-async 1")
    GDB_Engine.send_command("set pagination off")
    GDB_Engine.send_command("set non-stop on")
    GDB_Engine.send_command("attach " + str + "&")

def test():
    for x in range(0,3):
        global p
        time.sleep(0.5)
        GDB_Engine.send_command("find 0x00400000,+500,1")

def send_command(str):
    global p
    p.sendline(str)
    p.expect_exact("(gdb) ",async=False)
    print(p.before)

在main中,我简单地调用attach(),然后调用test()函数。结果是这样的:

find 0x00400000,+500,1
&"find 0x00400000,+500,1\n"
~"0x400006\n"
~"0x400014\n"
~"0x40002a\n"
~"0x400061\n"
~"0x400069\n"
~"0x4000a8\n"
~"0x4000b0\n"
~"0x4000d2\n"
~"0x4000da\n"
~"0x4000e8\n"
~"0x4000f2\n"
~"0x40012a\n"
~"0x40019a\n"
~"13 patterns found.\n"
^done


find 0x00400000,+500,1
&"find 0x00400000,+500,1\n"
~"0x400006\n"
~"0x400014\n"
~"0x40002a\n"
~"0x400061\n"
~"0x400069\n"
~"0x4000a8\n"
~"0x4000b0\n"
~"0x4000d2\n"
~"0x4000da\n"
~"0x4000e8\n"
~"0x4000f2\n"
~"0x40012a\n"
~"0x40019a\n"
~"13 patterns found.\n"
^done

find 0x00400000,+500,1
&"find 0x00400000,+500,1\n"
~"0x400006\n"
~"0x400014\n"
~"0x40002a\n"
~"0x400061\n"
~"0x400069\n"
~"0x4000a8\n"
~"0x4000b0\n"
~"0x4000d2\n"
~"0x4000da\n"
~"0x4000e8\n"
~"0x4000f2\n"
~"0x40012a\n"
~"0x40019a\n"
~"13 patterns found.\n"
^done

按预期工作,但如果我在函数 send_command() 中将参数 async 作为 True 传递,结果输出将变为如下所示:

=thread-group-added,id="i1"
~"GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1\n"
~"Copyright (C) 2014 Free Software Foundation, Inc.\n"
~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.  Type \"show copying\"\nand \"show warranty\" for details.\n"
~"This GDB was configured as \"x86_64-linux-gnu\".\nType \"show configuration\" for configuration details."
~"\nFor bug reporting instructions, please see:\n"
~"<http://www.gnu.org/software/gdb/bugs/>.\n"
~"Find the GDB manual and other documentation resources online at:\n<http://www.gnu.org/software/gdb/documentation/>.\n"
~"For help, type \"help\".\n"
~"Type \"apropos word\" to search for commands related to \"word\".\n"
=cmd-param-changed,param="disassembly-flavor",value="intel"

=thread-group-added,id="i1"
~"GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1\n"
~"Copyright (C) 2014 Free Software Foundation, Inc.\n"
~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.  Type \"show copying\"\nand \"show warranty\" for details.\n"
~"This GDB was configured as \"x86_64-linux-gnu\".\nType \"show configuration\" for configuration details."
~"\nFor bug reporting instructions, please see:\n"
~"<http://www.gnu.org/software/gdb/bugs/>.\n"
~"Find the GDB manual and other documentation resources online at:\n<http://www.gnu.org/software/gdb/documentation/>.\n"
~"For help, type \"help\".\n"
~"Type \"apropos word\" to search for commands related to \"word\".\n"
=cmd-param-changed,param="disassembly-flavor",value="intel"

=thread-group-added,id="i1"
~"GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1\n"
~"Copyright (C) 2014 Free Software Foundation, Inc.\n"
~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.  Type \"show copying\"\nand \"show warranty\" for details.\n"
~"This GDB was configured as \"x86_64-linux-gnu\".\nType \"show configuration\" for configuration details."
~"\nFor bug reporting instructions, please see:\n"
~"<http://www.gnu.org/software/gdb/bugs/>.\n"
~"Find the GDB manual and other documentation resources online at:\n<http://www.gnu.org/software/gdb/documentation/>.\n"
~"For help, type \"help\".\n"
~"Type \"apropos word\" to search for commands related to \"word\".\n"
=cmd-param-changed,param="disassembly-flavor",value="intel"

它只打印第一个命令执行的结果(即p=pexpect.spawnu('sudo gdb --interpreter=mi')。为什么会发生这种情况?正确用法是什么异步属性?

最佳答案

来自 pexpect 文档:

On Python 3.4, or Python 3.3 with asyncio installed, passing async=True will make this return an asyncio coroutine, which you can yield from to get the same result that this method would normally give directly. So, inside a coroutine, you can replace this code:

index = p.expect(patterns)

With this non-blocking form:

index = yield from p.expect(patterns, async=True)

参见coroutines .

关于python - 正确理解 pexpect 的异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35372575/

相关文章:

python : using isinstance on C based type MatchObject

python - Pandas:向量化局部范围操作([i:i+2] 行的最大值和总和)

python - 奇数和代码问题

python - 使用 Tensorflow 作为 Anaconda 的环境

python - Python 中的 *tuple 和 **dict 是什么意思?

python - 如何删除数据类属性

javascript - Node.js 处理异步

python - 我们可以在kivy中使用matplotlib.animation吗?如果是,如何?

javascript - Firebase 函数返回未定义的值,即使有 promise

javascript - NodeJS 请求 mysql DB 以异步方式返回值