Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 理解pexpect';s正确地异步_Python_Python 3.x_Asynchronous_Pexpect - Fatal编程技术网

Python 理解pexpect';s正确地异步

Python 理解pexpect';s正确地异步,python,python-3.x,asynchronous,pexpect,Python,Python 3.x,Asynchronous,Pexpect,我正在尝试使用pexpect编写一个gdb前端来与gdb/mi通信。我是pexpect的新手,正在尝试了解expect\u exact()函数的异步属性。我编写了一个简单的测试,如下所示: def attach(str): global p p=pexpect.spawnu('sudo gdb --interpreter=mi') p.expect_exact("(gdb) ") GDB_Engine.send_command("set target-async

我正在尝试使用pexpect编写一个gdb前端来与gdb/mi通信。我是pexpect的新手,正在尝试了解expect\u 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)
=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"
大体上,我只是调用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传递,结果输出如下:

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)
=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"
=已添加线程组,id=“i1”
~“GNU gdb(Ubuntu 7.7.1-0ubuntu5~14.04.2)7.7.1\n”
“版权(C)2014免费软件基金会,公司”
~“许可证GPLv3+:GNU GPL版本3或更高版本\n这是免费软件:您可以自由更改和重新发布。\n在法律允许的范围内,不提供任何保修。有关详细信息,请键入\“显示复制\ \和\“显示保修\”。\n”
~“此GDB已配置为\“x86_64-linux-gnu\”\n键入\“显示配置\”以获取配置详细信息。”
~“\n有关错误报告说明,请参阅:\n”
~“\n”
~“联机查找GDB手册和其他文档资源,网址为:\n。\n”
~“有关帮助,请键入\“帮助\\n”
~“键入\“apropos word\”以搜索与\“word\”相关的命令\n”
=cmd param changed,param=“反汇编风格”,value=“英特尔”
=已添加线程组,id=“i1”
~“GNU gdb(Ubuntu 7.7.1-0ubuntu5~14.04.2)7.7.1\n”
“版权(C)2014免费软件基金会,公司”
~“许可证GPLv3+:GNU GPL版本3或更高版本\n这是免费软件:您可以自由更改和重新发布。\n在法律允许的范围内,不提供任何保修。有关详细信息,请键入\“显示复制\ \和\“显示保修\”。\n”
~“此GDB已配置为\“x86_64-linux-gnu\”\n键入\“显示配置\”以获取配置详细信息。”
~“\n有关错误报告说明,请参阅:\n”
~“\n”
~“联机查找GDB手册和其他文档资源,网址为:\n。\n”
~“有关帮助,请键入\“帮助\\n”
~“键入\“apropos word\”以搜索与\“word\”相关的命令\n”
=cmd param changed,param=“反汇编风格”,value=“英特尔”
=已添加线程组,id=“i1”
~“GNU gdb(Ubuntu 7.7.1-0ubuntu5~14.04.2)7.7.1\n”
“版权(C)2014免费软件基金会,公司”
~“许可证GPLv3+:GNU GPL版本3或更高版本\n这是免费软件:您可以自由更改和重新发布。\n在法律允许的范围内,不提供任何保修。有关详细信息,请键入\“显示复制\ \和\“显示保修\”。\n”
~“此GDB已配置为\“x86_64-linux-gnu\”\n键入\“显示配置\”以获取配置详细信息。”
~“\n有关错误报告说明,请参阅:\n”
~“\n”
~“联机查找GDB手册和其他文档资源,网址为:\n。\n”
~“有关帮助,请键入\“帮助\\n”
~“键入\“apropos word\”以搜索与\“word\”相关的命令\n”
=cmd param changed,param=“反汇编风格”,value=“英特尔”

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

来自
pexpect
文档:

在安装了
asyncio
的Python3.4或Python3.3上,传递
async=True
将使此返回成为
asyncio
corroutine, 您可以从中得到与此方法相同的结果 通常直接给出。因此,在协同程序中,您可以替换以下代码:

使用此非阻塞形式:


请参阅。

以简化问题(将重点放在
async=True
),使用一个伪Python子进程,而不是
gdb
。谢谢,我会在修复程序中的其他严重问题后尽快尝试。我最终再次回到这个问题,尝试了一下,但出现了以下错误:
无法隐式地将“bytes”对象转换为str
。我检查了internet,发现了这个问题,尽管他们说ey修复了它,我仍然得到了错误。有没有办法在不更改库代码的情况下手动修复此问题?@Desertricker它与
async=True
问题无关。如果您有新问题,请分别提问。请参见。顺便说一句,异步功能似乎只适用于同一进程的多个不同管道,但我想获取输出同样的管道。所以我想我要找的东西有点不同,但我会接受你的答案,因为它是正确的和有用的。哦,顺便说一句,我是指多个不同的繁殖,而不是过程抱歉。只是想说清楚。