问题 如何调用类中包含的异步函数?


基于 这个答案 我想在一个类中构建一个异步websoket客户端,该类将从另一个文件导入:

#!/usr/bin/env python3

import sys, json
import asyncio
from websockets import connect

class EchoWebsocket:
    def __await__(self):
        # see: https://stackoverflow.com/a/33420721/1113207
        return self._async_init().__await__()

    async def _async_init(self):
        self._conn = connect('wss://ws.binaryws.com/websockets/v3')
        self.websocket = await self._conn.__aenter__()
        return self

    async def close(self):
        await self._conn.__aexit__(*sys.exc_info())

    async def send(self, message):
        await self.websocket.send(message)

    async def receive(self):
        return await self.websocket.recv()

class mtest:
    async def start(self):
        try:
            self.wws = await EchoWebsocket()
        finally:
            await self.wws.close()

    async def get_ticks(self):
        await self.wws.send(json.dumps({'ticks_history': 'R_50', 'end': 'latest', 'count': 1}))
        return await self.wws.receive()

if __name__ == '__main__':
    a = mtest()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(a.start())

我导入它 main.py,我有以下内容:

from testws import *

a = mtest()
print (a.get_ticks())
print ("this will be printed after the ticks")

但它检索到以下错误:

root@ubupc1:/home/dinocob# python3 test.py
<coroutine object hello.get_ticks at 0x7f13190a9200>
test.py:42: RuntimeWarning: coroutine 'mtest.get_ticks' was never awaited
  print (a.get_ticks())
this will be printed after the ticks

这是怎么回事?为什么我无法访问mtest.get_ticks如果它有 async 在开头的一句话 def


9559
2018-02-02 17:59


起源

你需要使用 await 当你打电话给它。 - dirn
通过这种方式调用函数时出现了synthax错误: foo = await a.get_ticks()... - harrison4
你不能用 await 一个协程之外。如果您尝试在另一个协程之外执行协程,则需要使用事件循环来安排协程(例如, loop.run_until_complete(a.get_ticks()))。 - dirn
好的,但是我怎样才能将get_ticks的返回值设置为变量?你能告诉我一个你在说什么的实例吗? :) - harrison4
foo = loop.run_until_complete(a.get_ticks()) - dirn


答案:


最后我找到了正确的方法(特别感谢 @dirn

#!/usr/bin/env python3

import sys, json
import asyncio
from websockets import connect

class EchoWebsocket:
    async def __aenter__(self):
        self._conn = connect('wss://ws.binaryws.com/websockets/v3')
        self.websocket = await self._conn.__aenter__()        
        return self

    async def __aexit__(self, *args, **kwargs):
        await self._conn.__aexit__(*args, **kwargs)

    async def send(self, message):
        await self.websocket.send(message)

    async def receive(self):
        return await self.websocket.recv()

class mtest:
    def __init__(self):
        self.wws = EchoWebsocket()
        self.loop = asyncio.get_event_loop()

    def get_ticks(self):
        return self.loop.run_until_complete(self.__async__get_ticks())

    async def __async__get_ticks(self):
        async with self.wws as echo:
            await echo.send(json.dumps({'ticks_history': 'R_50', 'end': 'latest', 'count': 1}))
            return await echo.receive()

这在main.py中:

from testws import *

a = mtest()

foo = a.get_ticks()
print (foo)

print ("async works like a charm!")

foo = a.get_ticks()
print (foo)

这是输出:

root@ubupc1:/home/dinocob# python3 test.py
{"count": 1, "end": "latest", "ticks_history": "R_50"}
async works like a charm!
{"count": 1, "end": "latest", "ticks_history": "R_50"}

欢迎提出任何改进建议! ;)


13
2018-02-03 00:05