Python调试器不能步进到协程中?

5
在下面的示例中:
import asyncio
import ipdb

class EchoServerProtocol:
    def connection_made(self, transport):
        self.transport = transport

    def datagram_received(self, data, addr):
        message = data.decode()
        print('Received %r from %s' % (message, addr))
        print('Send %r to %s' % (message, addr))
        self.transport.sendto(data, addr)

loop = asyncio.get_event_loop()
ipdb.set_trace(context=21)
print("Starting UDP server")
# One protocol instance will be created to serve all client requests
listen = loop.create_datagram_endpoint(    EchoServerProtocol, local_addr=('127.0.0.1', 9999))
transport, protocol = loop.run_until_complete(listen)

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

transport.close()
loop.close()

我正试图进入

loop.create_datagram_endpoint( EchoServerProtocol, local_addr=('127.0.0.1', 9999))

了解它的内部行为。 然而,当我尝试进入协程时,调试器跳过它,就好像按下了n而不是s

> ../async_test.py(18)<module>()
     17 # One protocol instance will be created to serve all client requests
---> 18 listen = loop.create_datagram_endpoint(    EchoServerProtocol, local_addr=('127.0.0.1', 9999))
     19 transport, protocol = loop.run_until_complete(listen)

ipdb> s
> ../async_test.py(19)<module>()
     18 listen = loop.create_datagram_endpoint(    EchoServerProtocol, local_addr=('127.0.0.1', 9999))
---> 19 transport, protocol = loop.run_until_complete(listen)
     20 

ipdb> 

这种行为出现在 PyCharm(2016 2.3 社区版)IDE 中。

我希望能够结束 这里,并能够进一步地跟踪代码。

1个回答

4

如果你像这样调用协程——使用await或者yield from语句,那么它就会正常工作。

listen = await loop.create_datagram_endpoint(EchoServerProtocol, 
                                             local_addr=('127.0.0.1', 9999))

在你的示例中,listen不是协程执行的结果,而是协程实例本身。实际的执行是通过下一行代码loop.run_until_complete()进行的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接